Django URLs from different apps in just the base one - python

So I have 2 apps for my django project. In the default mysite folder, you have the urls.py file. I cant seem to find out how to write all my urls for all the apps in just the one urls.py file.
I have tried this:
from reviews.models import *
however that isnt working.
Thanks for any help!

Just realised that i was importing the models instead of the views.
the code above should be from reviews.views import *

Try and prevent confusion with explicit view names, explicit is better than implicit
from reviews import views as reviews_views
from other_app import views as other_app_views

Related

What does admin.autodiscover actually do?

I've long been curious about what
admin.autodiscover()
actually do. The document didn't say much about it:
Above we used admin.autodiscover() to automatically load the INSTALLED_APPS admin.py modules.
and
There is really no need to use autodiscover when using your own AdminSite instance since you will likely be importing all the per-app admin.py modules in your myproject.admin module.
If I don't uncommnet
# admin.autodiscover()
what functionality I will lose?
And for what consideration should I use or not use autodiscover?
As u said: autodiscover() load all admin.py from the apps folders. So you have in the /admin/ all the models that you use (from your own app or not).
I recommend to use autodiscover() if you are going to use the admin app.
P.D. additionally some app have their on autodiscover with more functionalities.
admin.py is executed whenever your django loads URLconf from urls.py, the autodiscover() will search for all the apps in INSTALLED_APPS one by one and executes the code in that file.

Django application override & import path?

Let's have a django project using a 3rd party application.
I'd like to override some of its modules without touching original files.
Simple subclassing is not possible here, need to override code transparently as many other apps rely on original class names and functions.
Project's structure looks like:
django_project/
__init__.py
settings.py
overrides/ <-- here is a subdir with apps overrides
__init__.py
payment/ <-- here is an example of app to override
__init__.py
admin.py
forms.py <-- this file is ignored, original is imported
models.py
tests.py
views.py
settings.py was modified with
INSTALLED_APPS=(
'satchmo_store.shop'
#'payment' # original values
'overrides.payment' # modified app
...
)
The above solution however does not work, because Django does not insert path of added app into modules search path (sys.path).
Django just loads admin.py, models.py, tests.py and views.py, other files like forms.py are ignored.
Is this behaviour documented somewhere ? What exactly placing a module name in INSTALLED_APPS does behind scenes ?
I hacked the situation with hardcoding new modules search path in manage.py and Apache's setting of WSGIPythonPath.
import os.path
import sys
DIRNAME = os.path.dirname(__file__)
APPS_OVERRIDE = os.path.join(DIRNAME, 'overrides')
if not APPS_OVERRIDE in sys.path:
sys.path.insert(1, APPS_OVERRIDE)
I doubt this is the right way. Cann't find a guide describing apps overriding.
So, how can I properly override external Django application in my project ?
The bonus question: Do I need to copy whole application directory tree, not just particular files which are really modified ? As far as I know, Python stops at first matching module path, so it won't import other modules available in following parts of the search path.
Example of how to override your form:
overrides/payment/forms.py
from django import forms
class YourNewFormThingy(forms.Form): pass
overrides/payment/models.py
from satchmo.payment import forms as satchmo_payment_forms
from . import forms
satchmo_payment_forms.SomeForm = forms.YourNewFormThingy
Try including payment as well along with override.payment as satchmo uses payment module to process payments and payment code is flexible enough to include your code as well.

Import Error on server, but working OK in local machine

I have the following project structure:
apps(python package)
|
|
|----------trips(python package)
|----__init__.py
|----urls.py
|----views.py
project
manage.py
urls.py file has the following imports:
from django.conf.urls import patterns, url
from django.contrib.auth.decorators import login_required
from . import views
The app works perfectly fine when running on a local machine (I've made lots of apps like this, so I don't think the mistake is that obvious, but who knows)
When I upload this to a production server (Openshift with python 3), I see Django's debug template, stating an ImportError ocurred:
ImportError at /
cannot import name views
The line of the Exception is:
from . import views
I've tried also:
from apps.trips import views
with no luck...
Any ideas?
Update:
This is my Python path:
Python Path:
['/var/lib/openshift/52a6379ae0b8cd1b10000001/app-root/runtime/repo',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/distribute-0.6.49-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/pip-1.4.1-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/app-root/runtime/repo',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/psycopg2-2.5.1-py3.3-linux-x86_64.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/Django-1.6-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/django_selectable-0.7.0-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/django_autocomplete_light-2.0.0a4-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/six-1.4.1-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/South-0.8.4-py3.3.egg',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/opt/lib/python33.zip',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/opt/lib/python3.3',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/opt/lib/python3.3/plat-linux',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/opt/lib/python3.3/lib-dynload',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/virtenv/venv/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/python/opt/lib/python3.3/site-packages',
'/var/lib/openshift/52a6379ae0b8cd1b10000001/app-root/runtime/repo/wsgi/hector_transporte']
The last line points to my project folder (the one that contains the apps module). Another strange thing is django seems to be able to find my settings.py
I'm using the python 3 cartridge because "it's the present and future of the language"
Is there a reason you need to use the python 3 cartridge at this time? We have a new one coming out in about 3 weeks that will be more standard. We even have a quickstart for django with the 2.7 cart - github.com/openshift/django-example
I don't see the reference to {python_home}/Lib/site-packages/django. You might try adding that to your path.
ImportError can arise when there is a problem with the module you are importing. In this case, one of the dependences for the views module was missing on the server.

Can a django app have more than one views.py?

I have just started learning Django. I was wondering if Django app can have more than one views file? Let's say, I have two separate classes. Should I keep them in one views file or can I make two views files?
Thanks in advance!
Yes, you can. A modular way of splitting would be to create a package - views/
- views/
- first.py
- second.py
- __init__.py
and in your __init.py__ add the following:
from .first import *
from .second import *
This way, all your views would be available for urls.py.
Views are just python modules, you can do whatever you want, for instance you can change their names to whatever.py as long as your imports are correct :)
And as suggested: find more info here Django: split views.py in several files :)
You can totally do that, it is only a convention to use views.py.
Now, the question is: do you really need to create a new file to put your views inside ? Shouldn't these regrouped in a new application ?
Think of an other person reviewing your code: would the reason of the separation be crystal clear to him ?

Is there any way to activate django admin site without admin.py

I have many apps and i want to activate the admin for all the models in my all apps.
I remember few months one of my friend did something that enables the admin site without any admin.py file
he did something in settings.py files with INSTALLED_APPS and all of the apps showed in the admin section
I am now not able to find that. Any one??
Your friend probably did something like...
from django.db.models import get_models
for model in get_models():
admin.site.register(model)
In one of his admin.py files.
I dunno, I'd only do this to test stuff. It's a bit too magical. Remember you'll have to explicitly unregister any models you may want to register again.
Just follow the instructions in the docs. They describe how to activate the django admin site here. You need to modify urls.py. That's it.
There are even comments in that file that tell you which three lines to uncomment.
I recommend you do include an admin.py in your apps because it is the Django convention and that some explicitness in what you trying to achieve is a good thing; the solution I use is the following default admin.py template for all new apps I start:
# Auto registers any new models with the admin, eventually you will want a tailored admin.py
from django.contrib import admin
current_app = models.get_app(__package__)
for model in models.get_models(current_app):
admin.site.register(model, admin.ModelAdmin)

Categories