Remove Django url namespace and serve app from root urls.py - django

I have a Django app which I created as a reusable app with its own urls.py. The app's urls.py is included from the project's root urls.py:
url(r'^app/', include('app.urls', namespace='app')),
The full urls are example.com/app/view1, example.com/app/view2 . I want to remove the /app/ part from the urls so the views can be called by urls directly following the domain name such as exmpale.com/view1, example.com/view2. I refer to the urls in templates and views as 'app:urlname'.
What's the best way to accomplish this? Ideally without having to edit all of my templates.

Related

Universally usable files for django project - Django

I am making a django project and within the project I have 3 different apps. registration, users, groups. I dont want to have to print all of the import statements in the views.py file for each app. I also dont want to have to retype any of the methods for functions in each of the views.py file for small functions like generatingRandomNumbers. Is there a way to create a single file within the project directory that has all of the import statements or all of the methods that i can call at the beginning of each views.py file....
Also where would i put the file if possible, and how can i call the fill with all of the import statements at the top of each of the views.py file so i dont have to keep retyping all of it...
Here is my current directory:
Another question is how can i setup the urls in a way that i can redirect to any url within the project no matter which app it is localated in... Django documentation does not explain any of this at all...
an example would be a view in the registration app redirects to a url in the users app.. Here is what i have:
registration -> urls.py
urlpatterns = [
url(r'^$', views.Signup, name='user_signup'),
url(r'^setup_profile/$', views.ProfileSetup, name='profile_setup'),
]
users -> urls.py
urlpatterns = [
url(r'^home/$', views.UserHome, name='home_page'),
]
redirect statement from the registrations -> views.py file:
return redirect('user_home')

Redirect to different App view directory from urls.py

I have URL definitions in my wagtail blog's app directory(blog/urls.py), and I would like to reference a view in my search app directory (search/views.py). The current URL definition is url(r'^search/$', search, name='search'). I don't want to duplicate common search views in every app's views file. How do I format the URL in my blog app to use the search view in search/views.py?
Assuming that the blog app and search app are in the same level in project path directory,
You can use relative import like this:
from django.conf.urls import url, patterns
from search.views import search
urlpatterns = patterns(
url(r'^search$', search , name='search'),
)

Template URL redirection generating ImportError "no module named" error in Django 1.6

I'm trying to migrate my application from Django 1.0 to 1.6 and am running into a problem rendering templates with URL redirections.
My application structure is:
mysite
settings.py
urls.py
myapp
urls.py
views.py
etc.
myapp.urls contains:
from mysite.myapp import views
urlpatterns = patterns('',
# Login/Logout/Registration
url(r'^login/$', views.login, name="login"),
url(r'^registration/$', views.registration, name="registration"),
)
When I browse to /login, the URL is mapped correctly and invokes the view. However, when the view attempts to render its template, it generates this error:
ImportError at /login
No module named myapp
and points to this redirection syntax in the template:
<a href="{% url 'registration' %}">
I can browse to /registration without errors, so it has something to do with resolving back to the URL.
It must be something simple, but I'm stumped. Any suggestions would be greatly appreciated.
Finally tracked down the problem. What I had coded was correct. However, there were some other url patterns at the bottom of my url.py file that I had not converted to 1.6. For example
url(r'/schedule/$', 'schedule_list'),
And when the URL resolver looked for a reverse match, it apparently looped through the url patterns starting at the bottom and errored immediately. I guess I assumed it would either start at the top of the url patterns or there was some sort of lookup table that mapped names back to urls.
Project layout was changed in django 1.4.
Delete mysite part of the module name in myapp/urls.py. It should be:
from myapp import views

Should every django app within a project have it's own urls.py?

I am working on a django project which will contain several apps. Each app will have their own set of models and views.
Should each app also define their own url's with a urls.py or maybe a function. What is the best practice for defining urls of apps within a django project, and integrating these urls with the main urls.py (root url conf) ?
It depends. If you're dealing with a tiny website with one app, you can keep all the expressions in the same urls.py.
However, when you're dealing with a more complicated site with truly separate apps, I prefer the following structure:
myapp
admin.py
forms.py
models.py
urls.py
views.py
manage.py
settings.py
urls.py
Don't forget each folder needs it's own __ init__.py
# urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Notice the expression does not end in $,
# that happens at the myapp/url.py level
(r'^myapp/', include('myproject.myapp.urls')),
)
# myapp/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('myproject.myapp.views',
(r'^$', 'default_view',
(r'^something/$', 'something_view',
)
You also may want to look at Class-based Generic Views
If your app is going to display anything to the user with its own url pattern, it should probably have its own urls.py file. So in your base urls file, you'd have something in your urlpatterns like url(r'', include('path.to.app.urls')). Then your app's urls.py file would have a pattern like url(r'^$', 'path.to.app.views.view').
If the app is mostly self-contained and having its own place in the URL hierarchy makes sense, then it should have its own urls.py. But even if it does exist, it's still only a guideline to the project developer unless include() is used to graft it into the project URLconf.

What's the best way to map the main urls in a django project?

I've got a django project that contain some apps. The main urls.py includes the urls.py from the apps I've enabled, and all is good.
Now I want to configure the project so that when you go to http://testsite/, you'll get the same page that you get when you go to http://testsite/app/.
I can do this by duplicating the corresponding line in the apps urls.py in the projects urls.py, but this feels dirty.
Anyone know a better way?
Set up a redirect_to from the first url to the second, ie:
from django.conf.urls.defaults import patterns, url, include
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
# Example:
url(r'^$', redirect_to, {'url':'/app/'}),
url(r'^app/', include('app.urls')),
# ...
)
HTH
A redirect is the way to go, because you don't want multiple canonical URLs for the same resource (wastes Google juice). Ideally, you should do the redirect as close to the edge of your stack as possible to conserve server resources. So you can do a Django redirect_to urlconf entry, but you'd be better off with an Apache or nginx or insert-your-webserver-here redirect, so the initial request never has to hit Django at all.