[Django]-Should my Django site's main page be an app?

9πŸ‘

βœ…

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.

πŸ‘€Forthaction

11πŸ‘

I usually start with 3 apps (call them whatever you want):

  • layout – basic layout: some common static files and a basic-template (other templates extend this one, main purpose is to
    include a common html <head> ). Also contains the home-page and some other simple pages.

  • config – contains the project settings / configuration, and the main urls.py (and in my case also wsgi.py)

  • myapp – the actual app I want to create.

This nicely separates functionalities. Often I can just re-use the base app for other projects.

   $ ./manage.py startproject config
   $ ./manage.py startapp layout
   $ ./manage.py startapp myapp 
πŸ‘€Alex

0πŸ‘

If you need to render just one page then you can do something like this (from Django documentation) inside global urls.py

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path("home/", TemplateView.as_view(template_name="home.html")),
]

And then you make your home.html in root project tempalates to which you point from your settings.py and that should be it. No need for separate app….

πŸ‘€Ilija

Leave a comment