Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring Django Project

Tags:

python

django

I know that you're supposed to have a templates folder within each app folder, how I have two questions clarify further. 1, Should I have 2 base.html files (one in each app) that are identical? This seems like it's creating more files than need be... and 2. What about the static and media folders? Should I also have two of each or should they be at the project and app folders level?

If there is supposed to be a static folder in each app folder then do I have two css files? I feel like that makes no sense since the css can cover things that overlap from app to app.

I'm also wondering if have it setup the way I currently have it will effect anything, or if "best practice" is more so just for if you're working on a project with multiple people (which I'm not, in which case should I care?)

Here is my current structure:

/evverest/
    /evverest/
    /feed/
    /users/
    /blog/
    /templates/
        /base/
        /feed/
        /users/
    /static/
        /css/
    /media/
        /post_pics/
        /profile_pics/
like image 926
Garrett Avatar asked Sep 17 '17 20:09

Garrett


People also ask

How many apps should a Django project have?

Django comes with six built-in apps that we can examine.

What is a Django directory?

Django root directory is the default app which Django provides you. It contains the files which will be used in maintaining the whole project. The name of Django root directory is the same as the project name you mentioned in django-admin startproject [projectname].

What is the architecture of Django?

As mentioned, Django follows the MVT framework for architecture. MVT is generally very similar to that of MVC which is a Model, View, and Controller. The difference between MVC and MVT here is the Django itself does the work done by the controller part in the MVC architecture.


1 Answers

This can be a little bit confusing. I like to do this:

  1. Global templates should live in the main templates/ folder. This should include base.html and any other templates from other plugins that you're overriding.
  2. App-specific templates should live in their own templates/ folder inside the app, e.g. blog/templates/blog/blog_index.html. These templates should {% extend 'base.html' %} so that all apps in the project are using the same base template, which is in the global templates folder.
  3. Assets should live in your global STATIC_ROOT folder from your Django settings, which I've called assets below. I'd stick to using the one folder for simplicity.

Here's an example of how this would look:

./evverest
├── evverest/
├── feed/
│   ├── templates/feed/feed_index.html
│   └── models.py
├── users/
│   ├── templates/users/users_index.html
│   └── models.py
├── templates/
│   └── base.html
├── static/
│   └── css/main.css
└── manage.py

For more on best practices, check out cookiecutter-django. There's quite a lot to learn, but they're interested in following best practices, so it's a great resource.

Hope this helps!

like image 165
Franey Avatar answered Sep 29 '22 03:09

Franey