Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Template outside Django Project Directory

I have a Django project that is going to use React as the frontend. The project structure is as follows:

ProjectName/
├── api/
│   └── otherapp1/
│   └── otherapp2/
│   └── api/
│      └── views.py (will contain the method for loading react page)
│      └── settings.py
│   └── manage.py
└── frontend/
    └── public/
        └── index.html (react root page)

How can I load the index.html page with Django so I can use django for authentication and cookie/session management? I can't figure out how to configure the template/template dirs properly since it's outside the root folder for the django part of the project.

The boilerplate I was using is here

It might be relevant to note that since it is a docker project, there are 3 containers. One for the database, one for django, and one for the frontend. BASE_DIR in my django project resolves to /app/api. It might be that I need to access a directory in another container, but if I can't do that, then how should I go about linking Django to my React app?

like image 976
cclloyd Avatar asked Apr 30 '18 03:04

cclloyd


People also ask

Where should I put my templates in Django?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

How do I override a Django template?

To override these templates, you will need to have an admin folder in your templates folder. If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

Where are Django admin templates stored?

The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).


1 Answers

Update the TEMPLATE config in settings(api/api/settings.py):

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '../frontend/public')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Now Django would look for template files in frontend/public directory

like image 57
scene_contra Avatar answered Sep 21 '22 08:09

scene_contra