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?
A Django app is a small library representing a discrete part of a larger project. For example, our blog web application might have an app for posts , one for static pages like an About page called pages , and another app called payments to charge logged-in subscribers.
Both sources agree that you should create a separate app in the following situations: If you plan to reuse your app in another Django project (especially if you plan to publish it for others to reuse). If the app has few or no dependencies between it and another app.
1 Answer. The difference between Project and App in Django is that the Project could be defined as the entire application, containing apps to perform specific tasks. And Apps are within the Project that is self-sufficient in a project and are designed to perform specific tasks.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With