4👍
in my opinion you just need one app let’s call it web
since it is used for website only. The steps I follow in creation of a simple project after creating repository and database and make it running is:
- Create static, media and templates folders
python manage.py startapp web
- Add web in
INSTALLED_APPS
in settings.py file - Put html files in templates folder and other folders(js,css,images,fonts..) in
static
folder - Set URL to the new app in project_name > urls.py
- Create view for index page in
web.views
- Create url to the view just created in
web.urls
- Change every links of images, js and css files in index.html to
static
url as to use in python.{% load static %}
is must - The page will be loaded in localhost now.
- create other
views
and seturls
in web app to other pages like about,contact and so on - Create a list of models You will need and define them in
web.models
- pass them to admin page by using
web.admin
- Make it dynamic by passing data from models to templates.
hope you get it right
4👍
from my experience using Django, there is no definite answer for that it all depends on you and what you are comfortable with, I hope this helps.
Templates/static files
I put these templates and static files into global templates/static directory, not inside each app, If you are full-stack developer working alone or in a small team, you can create per-app templates/static directory. It’s really just a matter of taste.
[projectname]/
├── [projectname]/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
|
|── [App1]
|── [static]/
| ├── [css]
| ├── [Javascript]
| └── [Images]
|
|── [templates]
|── manage.py
└── requirements.txt
1👍
There isn’t exactly a definite answer: you can organize it however you feel necessary, and Django won’t force you to put things in a fixed place. Arguments to back up my point:
- Django’s template settings:
TEMPATES['DIRS']
. Once you put in a folder named ‘templates’, it will search through the entire project where a folder named templates is located. You can put it on the first level (next to manage.py), or five levels inside an application, it doesn’t matter. - Django’s
STATICFILES_DIRS
setting. If Django wants you to put your static files in a directory, it could’ve limited the arguments to a string, but it defaults to a list and you can stuff in as many as you want.
To me, I like the way how a book Packt publishes organizes the files: static
, site_static
, and templates
all on the base level.
Your application is small now, but once it gets larger, it will be annoying to go through each application to access, say, the templates. If you keep everything together, you can access all of them easily.
So it’s something like:
|- project
|- app1
|- app2
|- site_static // your custom static files
|- css
|- js
|- static // bootstrap, webpack stuffs, etc.
|- media // user uploads and images used in site
|- templates
|- manage.py
|- requirements.txt
Although keep static
and media
out of the sight though. It’s unnecessary to put on github and there’ll be privacy/copyright concerns regarding the images.
- [Django]-Django.core.exceptions.ImproperlyConfigured: Requested setting REST_FRAMEWORK
- [Django]-Django Foreign Keys Breaking with Multi-Table Inheritance
- [Django]-Dynamically alter Field choices in Django ModelForm