[PYTHON] I got stuck doing Django1.4, so I wrote a "memo" about absolute path and relative path.

background

I was studying to learn Django, but I stumbled around the path, so I thought it might be a problem and looked it up.

relative path How to call a file based on your current directory. Write a path based on the scenery you see from where you are

I'm practicing Django, so I'll explain it with reference to the following. The current directory looks like the following.

/Users/username/Projects/python/dj/mysite

This is the file structure of the mysite directory.

.


├── books
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── manage.py
├── mydb

Now I'm at the original. (Mysite directory). If you call admin.py in books with a relative path,

books/admin.py

Just do it. How easy is it?

absolute path Write the path based on the view from the root of the computer. Call regardless of where you are.

Using the same example as above,

/Users/username/Projects/python/dj/mysite

In short, open the terminal

pwd

It seems that I will enter the path that came out.

Still there is a mystery

After all, even if I investigated this and tried both, the mystery of the original could not be solved. Even though I went to the directory where the template exists and called it with the correct name

template does not exist

It was hard to keep being told.

After investigating for two days, I was worried

import os

PATH_PROJECT = os.path.realpath(os.path.dirname(__file__))

And at TEMPLATE_DIR

PATH_PROJECT + '/templates/'

I managed to solve it. It's still a mystery.

Postscript

I thought it was solved! It seems that the way to write the reference point of the absolute path is different, so if you call ** pwd ** with the shell and hit it, put the path that came out in TEMPLATE_DIR and it will be a great success!

There are things that can be summarized.

Recommended Posts

I got stuck doing Django1.4, so I wrote a "memo" about absolute path and relative path.
I got stuck when trying to specify a relative path with relative_to () in python
I got stuck so I reviewed it
I tried using pipenv, so a memo
A memo that I wrote a quicksort in Python
A memo about building a Django (Python) application with Docker
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error