In a large Django project, I have several evil monkey-patching hacks to execute at application startup time. However, I don't quite see a correct place for such hackery: neither urls.py nor settings.py nor manage.py seem fit for this. Where would you recommend I put those?
In python you will always come across initialization. That's why its always better to use init for initialization. Even in django when you create a project it must have an init.py in it.
I usually put all my initialization in __init__.py its a safe and clean way. You can do the same rather than create another initialization module.
There isn't really a good answer to this question at the moment. There's a Summer of Code project at the moment to rewrite the app loading process, which hopefully will include hooks for initialization code.
In the meantime, I think the best place for this is in urls.py. The admin application and Haystack both do it there, and it seems a good pattern.
Related
I would like to know if there is a best practice for the structure of Django projects.
In particular, where do you located the virtualenv folder for your project?
Do you think is better the following solution:
/myproject
/myenv
manage.py
/mysite
settings.py
urls.py
wsgi.py
__init__.py
Or do you think is better to have a folder with all environments, for example in the home directory:
/virtualenvs
/myproject1_env
/myproject2_env
...
/myprojectN_env
I like to use virtualenvwrapper to keep my virtualenv at the same place and easily access them. I can recommand you to use this :)
Features
- Organizes all of your virtual environments in one place.
- Wrappers for managing your virtual environments (create, delete, copy).
- Use a single command to switch between environments.
- Tab completion for commands that take a virtual environment as argument.
- User-configurable hooks for all operations (see Per-User Customization).
- Plugin system for more creating sharable extensions (see Extending Virtualenvwrapper).
I have no relation with this project, I just use it on daily basis and really like it. Hope it can help you :)
Yup, using virtualenvwrapper is the best practice to manage your project without interfering with your machine environment it's like creating your own environment or machine you can say.
I think Django has provide the best structure as we use daily without any interruption not even mentally and it's easy to handle, Everything is separated so well.
Yes and you can have many aaps under one project which may or may not be connected to each other depends on your requirement , configuration of all app under one project will be in settings.py which is in parent folder for all apps.
Basically it's awesome what they have provided.I will suggest you to use default hierarchy, doesn't matter even if your working with big or small project.
The django docs clearly states
You shouldn't alter settings in your applications at runtime.
Here's the link to that statement
My question is, why is this so? I want to add applications dynamically at runtime, and add databases at runtime, both of which involve editing the settings. Can someone explain why settings are not to be edited at runtime and if exceptions exist, which settings they are and why they are exceptional? I'm not so much interested in how to achieve my goal, but in the reason for why settings shouldn't be altered.
Most settings will not be re-read if you change them at runtime. So Django will not recognise the changes you make.
This is due to the fact that Django is just normal Python code. It isn't like a server that is monitoring your code - it is just part of your code.
In some cases, parts of Django code might respond to changes in settings, because they might do 'settings.DEFAULT_FROM_EMAIL' every time mail is sent, for example.
But if Django processes the setting in any way, like it has to do for INSTALLED_APPS, it isn't going to notice you changed something and re-do the processing.
Which settings are safe? Well, the docs are saying "none are safe", because it might change in the future. Django might save a copy of any setting for some reason, or do some processing.
Changing INSTALLED_APPS could never be made to work, because it alters which modules are imported. There is simply no way that Django could work around the way that Python works at this level - it would need to be able to 'unimport' modules, which is basically impossible (the only way is to restart the process), and there are other problems associated with cross-app links.
AFAIK there is no documentation on which settings are safely modifiable at runtime, but there is an open ticket asking that they be documented more clearly.
If you take a look under the hood at the settings object Django exposes to interface with your project's settings module, you'll notice that there's nothing preventing you to dynamically change the settings at runtime.
You should, however, appreciate that the framework's architecture is built around a request-response flow where a lot of global state is being shared between threads for memory optimization, based on the premise that the application is configured only once during initialization.
I am using mongoengine and would like to run connect() after settings (not inside them as suggested in its docs). This is actually more like a general question how to run code right after all settings are loaded.
Update: I need a solution for management commands as well. Common approach is adding a middleware with exception MiddlewareNotUsed or adding code to root urls.py, but both don't work for commands.
The normal place for startup-like code is in a urls.py (when you need the settings to be already loaded). Django doesn't have a good spot yet for this.
(There is an "app refactor" branch that a gsoc student worked on in 2011, but it didn't get merged into core django yet. This "app refactor" includes a solution to your very problem, but that doesn't help you...)
You mention that a management command also needs it. Is that your own management command? Nothing stops you from importing the urls.py there, is it?
This is sadly one of the few Django weak points. Luckily there aren't that many :-)
I've just started using Django and one thing I find that I'm doing is starting a lot of new projects. I'm finding this process to be pretty tedious every time, even using manage.py startproject * I'm constantly changing settings in settings.py like media_root and template paths. Just a little background, I come from PHP and CodeIgniter. I never used a stock CI directory. I modified it to meet my needs for a new project. When I needed a new project, I would just copy that directory. manage.py seems to generate the files on the fly so this approach doesn't seem that possible. Does anyone else have any advice on this?
Lincoln loop has some best practices, they suggest importing settings from a different file. http://lincolnloop.com/django-best-practices/projects/modules/settings.html
Also checkout pip requirements, you might be able to use this to install the settings module from an external source like a git repo.
I'm using Paver to automate my Django project setup.
I have a Bitbucket repository with my own bootstrap setup. Eventually I'll make this generic, but for now it might give you some example code
Sounds like you're starting new projects very often. I assume that's because you're learning. Sure, if there's a custom settings.py that will save you some typing as you generate your learning projects, create it and use it. You could make your template the whole project directory, but since you're unlikely to have a lot of project-level boilerplate outside of settings.py, just focus on that one file. The settings file is the essence of the project.
Django development is all about apps. As you learn more, apps will start to become your focus. My advice would be not to pour too much energy into making an efficient assembly line for project creation.
Also, please learn and use use version control. For bonus points, also learn and use virtualenv :)
I have created a complex E-R diagram for a django site I'm developing. It maps to 11 tables in the database. The site has many features, so I would like to split it into multiple apps. The Django manual said that Django apps should be pluggable, but if I split the models into many apps, they would be dependant on each other. Is this a good practice? If not, how should I structure my application?
Thanks
I wouldn't worry over the statement about making apps pluggable. Sure, if it could be a useful app in other projects, you may want to - but nothing enforces this.
There is no harm in making internal apps dependent.
Personally, my project-specific apps live inside the project module (or for larger projects, inside a project.apps module). This way, you're not polluting the python import namespace with your one-time apps.
You could separate them out into self contained apps, and they would work within the context of your project.
You could also create each app so it's totally independent. This often takes a bit more work, a nice example of this is Django-tagging, which you can basically attach to any other object.
So yes, you can do it. However if the app is just for you it may not be worth the effort (IMHO) ;)