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.
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
- [Django]-How to add the current query string to an URL in a Django template?
- [Django]-Django how to partial render
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
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β¦.
- [Django]-A simple example of Django and CSS
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-What is a Django "app" supposed to mean?