[Django]-Simple Django Project Structure

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:

  1. Create static, media and templates folders
  2. python manage.py startapp web
  3. Add web in INSTALLED_APPS in settings.py file
  4. Put html files in templates folder and other folders(js,css,images,fonts..) in static folder
  5. Set URL to the new app in project_name > urls.py
  6. Create view for index page in web.views
  7. Create url to the view just created in web.urls
  8. Change every links of images, js and css files in index.html to static url as to use in python. {% load static %} is must
  9. The page will be loaded in localhost now.
  10. create other views and set urls in web app to other pages like about,contact and so on
  11. Create a list of models You will need and define them in web.models
  12. pass them to admin page by using web.admin
  13. 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:

  1. 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.
  2. 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.

Leave a comment