Issue
I have finished reading the Django official tutorial which teaches how to create a simple polls app, which is the way they chose to teach beginners the Django basics. My question is, now that I know how to make a simple app and want to create my own website (using Django), should the main (front) page of my website, be an app as well? If so, how do people usually call it and configure it? I mean, it should do nothing but render a html template, so why make it so complicated? If not, where do I put all the static files and how do I reference them? I am a bit confused and could use your help. Maybe I misunderstood Django's main use?
Solution
You can create your templates and static files in the root project folder where your manage.py
file lives. In the root folder create the following folders:
templates
(for HTML)static
(for CSS, JS and images)
In your settings.py
file, make these variables look like this:
TEMPLATES = [
{
...
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
...
},
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Note: STATICFILES_DIRS
variable is initially not present in the settings.py
file, but you can add that by your own. Django, by default, finds static files in the static
directory of each app. If you want Django to read the static
directory you created in the project root, you need to add this variable. Django official documentation reference: https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATICFILES_DIRS
To render these templates you can create views.py
in the directory where your settings.py
file lives and add the route in urls.py
in the same folder.
This is one of the several ways to achieve what you want. Hope you won't need to plug these templates (eg, your home page) to or say use these templates in any other project, otherwise do as Timmy suggested in the comment on your post.
Answered By - Forthaction
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.