简体   繁体   中英

Simple Django Project Structure

I have built the virtual environment and root directory for my django project, which will be a simple website for a event venue. The site will simply have a few different tabs on the navigation bar which shows the user some of the venue specifics which include pricing, a photo gallery, and the venue's history.

My problem is that there is so much conflicting information on the web concerning "The best practice for project structure".

Should I create an app for the home page and each of the pages that follow, or just create a core app that houses the majority of the project? If this is the case, is this project a good example? --> https://github.com/justdjango/django-ecommerce

Does anyone know of a simple project that I can reference?

Again this is a pretty simple project with only a few urls and no forms.

I would greatly appreciate anyone who has taken the time to read through this and help me.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM