Django template and version

Multi tool use
Multi tool use


Django template and version



I searched the web for the latest version of Django and it is 2.0.6 on the official Django documentation. But the versions that are given on the official page are just 2.0, 2.1, 1.9 etc. So I was just wondering if the documentation for 2.0 is for all version like 2.0.5, 2.0.6, 2.0.7 etc. Because there is no documentation for version 2.0.6. But again the version 2.0 is different and it is installable with pip.



Earlier I was using Django 2.0.6 and I had a problem with the template. I first started my project with name web and then made a app named music. Inside that I made a template directory and inside that another music folder and placed my index.html file inside that. so overall the path like this
web/music/template/music/index.html. Now I was using render from django.shortcuts.



I referenced the index.html file as music/index.html in the view.py file in music directory. But it is unable to find the file and it is just showing error.



I thought this could be a version problem.




2 Answers
2



Your folder structure should be like this:


music/ <-- higher level folder
|-- myproject/ <-- django project folder
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
|-- templates <-- Templates folder
| |-- music
| | |-- index.html
|-- static <-- Static folder
| |-- css
| |-- js
| |-- images
| +-- manage.py
+-- venv/ <-- virtual environment folder



Your project folder, app folder and Template folder should be together with your manage.py.



You also need to add this to your settings.py


TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")], <-- add this line
'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',
],
},
},
]



views.py


def index(request):
return render(request, "music/index.html", ) <-- Dont forget this here music is your folder's name





Why have you included myproject folder 3 times. It should only come 2 times. One as the project and other as the app or the homepage
– Nikhil Rathore
Jun 30 at 16:45





Yep sorry my bad let me update my answer
– Rohan Baddi
2 days ago





But in the official documentation it is mentioned to make a template folder inside our app which is polls in the documentation and here it is music. Inside this make another music folder and place the index.html inside that. So the whole path becomes project/app/template/app/index.html. We have to reference it as app/index.html as django sees the template folder by default. Also we don't need any changes in the settings. There is nothing writen in the documentation regarding any changes in the settings.py file. Can you pls explain the difference
– Nikhil Rathore
2 days ago






no it's not like project/app/template/app/index.html. docs.djangoproject.com/en/2.0/intro/tutorial03/… your template folder should be in path music/templates/music/index.html here first music is you project/templates/app/index.html. The app and templates should be in your project folder.
– Rohan Baddi
yesterday



Have you added your template directory to the settings file?


settings



As the documentation says:



Let’s say you’re trying to override the templates for a third-party application called blog, which provides the templates blog/post.html and blog/list.html. The relevant settings for your project would look like:


import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [ ..., 'blog', ..., ]
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, ... }, ]



The TEMPLATES setting and BASE_DIR will already exist if you created your project using the default project template. The setting that needs to be modified is DIRS.



These settings assume you have a templatesdirectory in the root of your project. To override the templates for the blog app, create a folder in the templates directory, and add the template files to that folder:


templates/
blog/
list.html
post.html



The template loader first looks for templates in the DIRS directory. When the views in the blog app ask for the blog/post.html and blog/list.htmltemplates, the loader will return the files you just created.



So, in order to add your own template folder, you should modify the DIRS setting.


DIRS





No. Do I need to do that. How?
– Nikhil Rathore
Jun 30 at 16:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

eSBzdDt7IHwR5,E3kYIDq7AVUX7OR4aJ yWpG1 Qtoy5m3Cnm325isEc0CluTbQtO5h1eWaIKutSGww946jXtu OO 9MFn e
tTz,3h718VCffOFWlzOlZoy0cAx6t,KIiw2SI6HYV qkcIn5Y L0bjNIZwhqRAINu4mAr FS vCgl3rhRbJPzls kKE3fFjrp jMk9oA5x

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya