Are there conventions for naming Django project, setting and main app folders? - django

My Django site currently looks like this:
my_store/ (project folder)
app_1/
__init.py__
admin.py
apps.py
...
app_2/
__init.py__
...
my_store/ (settings folder)
settings.py
urls.py
...
store/ (main app folder)
__init.py__
...
Where:
my_store is the name of my project
app_1 and app_2 are potentially reusable apps
store contains project-specific logic and configuration (likely not reusable)
Are there established conventions for giving distinct names to each of:
the project folder (my_store)
the settings folder (my_store)
the main app folder (store) -- I've seen a few examples of calling this "main"
De facto / popular conventions welcome, but documented / authoritative conventions preferred.

I like to follow the example of doordash who kindly published a blog post on operating django at scale.
https://blog.doordash.com/tips-for-building-high-quality-django-apps-at-scale-a5a25917b2b5
I think that it answers you specific question succinctly below, but also as some other tips.
The Django tutorial recommends a structure below:
mysite/
mysite/
__init__.py
polls/
__init__.py
But doordash recommends a different structure:
mysite/
mysite/
__init__.py
polls/
__init__.py
I hope that this helps and has some additional details to help clarify.

Related

Is possible to customize the models package path in Django

The default folder structure of a project in Django is something like:
myproject/
- myapp/
- models.py
- myproject/
- settings.p
Would it be possible to change that structure?
I'd like to do something like:
myproject/
- myapp/
- infrastructure/
- models.py
- myproject/
- settings.py
or
myproject/
- myapp/
- infrastructure/
- models/
- __init__.py
- some_model.py
- myproject/
- settings.py
I know I can make a models package and split the models into different files. But I'm looking to change also the name/path of that package.
Yes and no. In that case, your app will be myapp.infrastructure and it will need to be added to INSTALLED_APPS in settings.
Having models.py file and models/ folder is mostly same for python. In this case, you will need to import different models in the __init__.py file inside the models/ folder.
The idea is that somehow your models and other codes should be imported somewhere in the project for django management command to see them (ex: for preparing database migrations - makemigrations command). So as long as makemigrations can see your model, you can place them anywhere. It is just a python's way, not directly related to django framework.
As for the name of basic things (models, views, apps, signals, admin and etc.) - I think the only one required to be in its name is models. As it is used directly by its name in ORM. Check this: Django Applications Documentation
So, it is possible thanks to python's syntax, but it is not recommended by django. Django's design practices were created to make it easier for everyone to write a readable app where you can easily understand where things supposed to be and work easily without learning internal rules of the project that someone else designed. It is very important for project where there are many developers and new members can join any time after the project's start. So, unless you are well aware what you are doing, you are free to develop any custom design you want - after all, all you need is for python to be able to import your functions and classes in other parts of the project.

django app file structure

Below is a common app file structure:
app/
views.py
forms.py
urls.py
tests.py
I found it's getting hard to maintain the code once the code base is getting big. I am thinking to organize the code based on individual web page, so each web page will have its own view.py, form.py and test.py.
app/
views/
page1_view.py
page2_view.py
forms/
page1_form.py
page2_form.py
tests/
page1_test.py
page2_test.py
Is there any big issue I will run into?
No. Django understands packages and files.
Example if you use
app/
views/
page1_view.py
page2_view.py
__init__.py
you can reference the views as app.views.page1_views without any issues.
The short answer is no. In fact, I've often split views, tasks, and tests as you are doing.

project layout and project utilities/libraries?

I am a bit confused with the project layout created since Django 1.4.
I want to add some global utilities to be used through my whole project and apps.
I tried to add a utils package at the same level of my apps but Django does not see it.
mysite/
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
utils/
__init__.py
shortcuts.py
Any advice / best practice on how to use such a global utilities package ?
Thanks
Michael
Instead of adding it to PYTHONPATH which you have to do on each machine you install your project on, you can consider adding it to the django settings (mysite/settings.py) in INSTALLED_APPS variable:
INSTALLED_APPS=(myapp, utils)
This has the effect of adding utils to the python path, so you can do things like:
from utils import shortcuts

Django 1.4 project wide code ( template tags)

With 1.4 round the corner I thought I would give it a go but I have a question about the new project layout.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
I can see the logic here and I am a fan of the new layout however if I am having issues with adding project wide code.
For example:
I have created a new templatetag that is specific to this project and doesn't fit logically fit in any one of my apps. To me this should then fit within mysite however (as far as I can tell) this is not then processed. apptag.py (see below) is available but sitetag.py is not. Now, I am assuming this is because mysite not processed in the same way as an app(?).
manage.py
myapp/
templatetags
__init__.py
apptag.py
__init__.py
models.py
mysite/
templatetags
__init__.py
site.py
__init__.py
settings.py
urls.py
My question then is what is the right way to go about this issue? Should I create an app called 'core', 'mysitecore' or such like? I can add 'mysite' to INSTALLED_APPS but that just feels down right wrong. Or is there another option that I am missing here.
Thanks in advance.
Templatetags need to be inside an app that is included in INSTALLED_APPS. This is, in my opinion, a wart in Django that isn't fixed by the new layout. Your idea of using a core app - or, as I often do, utils - is the right one.

Deploy Django project: folder/project structure

My django project in eclipse has this project structure:
main-project-folder/
src/
main-app/
app1/
app2/
settings.py
manage.py
urls.py
__init__.py
media/
templates/
Can i deploy the project with this structure? In other words, is right way to put src and other folders (media, tempaltes, etc.) in the root folder of my server (where my domain is linked)?
Like:
my-server-folder/
src/
media/
...
I imagine that in my-server-folder i should put the entry point of project, but in my project i haven't an entry point in main-project-folder, or does django automatically redirect to an entry point of src/main-app folder (i think that it doesn't because i don't find any options that say to django to do it)?
Sure. That's a fine directory structure.
Keep in mind your web server isn't going to know what to do with the Django project unless you tell it. If your web server is Apache (which it probably is if you don't know) look here for instructions to set it up to run the Django app:
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
And here for WSGI:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
Django apps aren't like PHP where you just upload them to the web server and they work.