Django 1.4 project wide code ( template tags) - django

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.

Related

Proper directory structure for app versioning (Django)

I am currently starting work on a existing Django Project, and I need to make changes to the existing APIs. However, I can't change the existing code since old apps and sites have been using it, which might take a lot of time to change the structure, and might break current dependencies for those. So, for now, as asked, I need to maintain separate directories to change/review the existing code and achieve a versioning-like structure for Django apps. What might be the best way/structure to achieve this?
The current structure is simple as given by django-admin startproject
project_dir
|___project_name_dir
|___settings.py
...
|___app_1_dir
|___ __init__.py
|___ views.py, serializers.py
...
|___app_2_dir
...
So, I need to like create versioning type structure for app_1 here. What might be the best approach to do that?
You can create a new app, called api_v2.
But generally i put all applications inside an application folder in the project_name_dir:
project_dir
|___project_name_dir
|___settings.py
|___applications
|___api
|___ __init__.py
|___ views.py, serializers.py
|___api_v2
|___ __init__.py
|___ views.py, serializers.py
|___other_apps
|___ __init__.py
|___ views.py, serializers.py

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

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.

manage.py - not in project folder?

I'm trying out Django for the first time, and I'm trying to follow the tutorial provided by the django team.
After I've created a new project I get the following folder/file structure, just as the tutorial says I should:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
When I create an app I run:
python manage.py startapp polls
...which creates the app 'polls' in the same folder as the manage.py file - which gives me:
mysite/
manage.py
polls/
__init__.py
admin.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
This means the app I created gets created outside my project folder, which, of course, should not be the case. I have tried to move manage.py inside the project folder. But when I do that and run:
python manage.py syncdb
...I get the following error:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path?): No module named testproject.settings
I guess I could move the app manually to the project folder, but this is something I shouldn't have to do.
So, either something is wrong in the tutorial (which I have very hard to believe), or I'm missing something out here (more likely).
Thanks in advance.
This is the new proper layout. "mysite/mysite" is an app, and "mysite/polls" is an app. The "mysite" parent folder is your project folder.
#holyredbeard that is the correct layout are you reading the older documentation?
Useful reading: http://www.tdd-django-tutorial.com/blog/articles/2012/tutorials-updated-django-14-and-its-weird-new-fold/
Don't move the manage.py it should sit outside the apps and the project folder.
since 1.4 common layout example...
project_root/
project_name/
media/
static/
static_root/ (in production)
templates/some_app/foo.html (overriding some_app at project level)
/admin/some_app/some_model/change_list.html
(overriding admin changelist for some_app.models.some_model)
settings.py
settings_deployment.py
urls.py
some_app/
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
This is the official layout since version 1.4.
The rationale behind is explained well in the release notes:
https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
Do not move manage.py. In general you can expect that Django's own scripts always do the right thing, you never need to move any files just to get it working.

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