can i include the same app url conf within two different root?
I mean, i have this
(r'^event/', include('quip.apps.event.urls')),
but i would like to have that
(r'^event/', include('quip.apps.event.urls')), #display event e.g. event/my-event-slug
(r'^events/', include('quip.apps.event.urls')), #filter events e.g. events/today/somewhere
I need different behavior in my 'quip.apps.event.urls'. the only solution that come in my mind is to create two urls file, but i don't think is a quite really good solution.
(r'^event/', include('quip.apps.event.someurls')),
(r'^events/', include('quip.apps.event.otherurls')),
any ideas? i'm sure this is a silly question.
Yes you can. To keep it modular, I would make urls a package:
(r'^event/', include('quip.apps.event.urls.someurls')),
(r'^events/', include('quip.apps.event.urls.otherurls')),
Where the directory structure would be
event/
__init__.py
urls/
__init__.py
someurls.py
otherurls.py
Also, in urls/__init__.py you could do
from .someurls import *
from .otherurls import *
Related
I am working on a project in Django. On my views.py I need to make use of multiple helper functions. In order to keep my code clean, I am going to create another file to wrap all these functions. I was planning to call the file just functions.py or helpers.py.
Which is the good practice to add helper functions for Django views? Is there any kind of convention, rule or anything?
UPDATE: These functions are closely related to the app itself. They have no sense out of their app.
Thanks!
The cleanest way would be to create multiple files with only functions related to each other. Ideally, if they are app-agnostic, put them in a python package outside of the django app you are using.
Ie.
All functions related to users go to view_helpers/users.py,
All function related to json go to view_helpers/json.py
The directory structure would be like that
django_project/
main_django_app/
__init__.py
views.py
settings.py
...
view_helpers/
__init__.py
json.py
manage.py
I am new to Django and have a little problem with making all the project structure clear and organized and so on.. The MVC stuff in Django is quite easy to use when making a small project. However, when I am trying to get a new layer (application logic, like three-tier architecture) between views.py and models.py I have problem to do so.
I have the following structure:
mysite/
manage.py
app1/
models.py
views.py
logic/
__init__.py
Class1.py
parser.py
...
and I want to load into views.py stuff from Class1.py and parser.py.
How do I do it?
Neither of the following works:
from app1.logic import *
from app1.logic.Class1 import Class1
Also, it would help me if somebody could list me some example of a really huge django project. It looks like either lots of classes and .py files should be in every app folder or everything is in the models.py.. Both look a little disorganised and I'm sure there is a way to make it a little bit clearer (like in Zend or Symfony).
And, I'm working with Python3 and Django 1.5b2.
Thanks.
If Class1 or parser import from views, then you have a circular dependency. You'll need to move any shared code out into a third file.
You might consider though whether you need all those separate files under logic. In Python there's no requirement to have a class in its own file, so maybe you could have a single logic.py instead of a logic directory containing several files.
My django project has two environments - development and test. Today I carelessly overwrote the settings.py in test with the one in development. It took me a while to correct the settings in test, which could have been avoided if I have a good way to maintain the two sets of settings separately.
I was thinking to keep two separate copies of settings.py and rename/move them when needed. This, however, is kinda caveman approach. Are there any smarter ways of dealing with this problem?
At the end of your settings.py file, add this:
try:
from settings_dev import *
except ImportError: pass
Where settings_dev.py will contain the dev settings. And in your production env, do not push settings_dev (just ignore it in .gitingore or your source code versioning system.)
So, when ever settings_dev.py is present, the settings.py will be overwritten by the settings_dev.py file.
One more approach by setting the environment variable:
if os.environ.get('DEVELOPMENT', None):
from settings_dev import *
mentioned here: Django settings.py for development and production
I prefer the first one, it's simple and just works.
Split your settings as documented here:
https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments
I like Eclipse for a number of reasons. One of them is, when I'm doing a java project, I can copy and paste my project or rename it, to make a new one, and it will update all the names of everything.
I'm trying to do something similar for a django project.
I can set an APPNAME variable with the following code
APPNAME = os.path.basename(os.path.dirname(__file__))
and then I could import this in any file that needed to know the name of the application. This isn't an ideal solution however but it would work for my settings.py file as well as my urls.py files.
It won't work for situations where I need to import something from somewhere like so:
from myproject.someapp import forms
Is there a way for django/python to know what "myproject" is? Or can I use an import statement relative to the current app?
Or maybe there's a better way to copy django projects.
EDIT: I imagine there are also database dependencies as well that I'd have to deal with.
I follow a couple of rules to keep my applications portable. I'll list them below in the hope that someone finds them useful.
Include my apps in the PYTHONPATH rather than my projects. This way I can execute from app import forms rather than from project.app ....
Following #1, I always import from app only. This means I can reuse my apps in other projects without having to change import statements within the app or in other dependent apps.
If you stick to #1 and #2 you can generally copy and paste projects without too much trouble. You'll still have to modify settings.py though.
I haven't exactly found the answer to this, maybe there is no best one. Django docs are not clear on this thing.
I'm not sure what is a good practice to set up a django project. Sure I have re-usable apps. But I always need some glue-code.
I find myself always creating "core" app for each project, which usually serves homepape.
Then in project url config I do something like this:
(r'^/$', include(core.urls))
Is this the way to go? Or do you have a better idea?
I think it's a good idea to use a glue app/module that also contains further helper functions/reusable code (if any), however I'm not sure if that's the way the other djangonauts do these kind of stuff.
Also, in order to match the homepage I think that the correct regexp is r'^$'. With the above solution you propose, you'll have to be careful because every url defined in core.urls will be 'mounted' under your site's root directory.
Also, and for the case of the homepage I used something like this
(r'^$', 'apps.core.views.homepage')
just to distinguish this url. I guess it's just a matter of how one wants things organized.
Don't put the slash and the dollar. This is how I did it.
(r'^', include('core.urls')),
You're already on the right track. ;)
The approach you mention is a good one. I tend to stash away stuff like that into a views.py file in the root of the project. There is already a urls.py in the root folder.