Django: Issue with url conf - django

I'm trying to change my project to use class based views, and I'm running into a strange error with my url conf
I have a classed based view:
class GroupOrTeamCreate(AjaxableResponseMixin, CreateView):
model = GroupOrTeam
fields = ['name', 'description']
#success_url = reverse('home_page') # redirect to self
I have the last line commented out because if I don't, django complains that there are no patterns in my url conf.
To start, here's my base urls.py
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home_page'),
url(r'^administration/', include('administration.urls', app_name='administration')),
url(r'^reports/', include('reports.urls', app_name='reports')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Clearly there are patterns in there. If I comment out the administration urls, it works. So I assume the problem is in there somewhere.
administration urls.py
from django.conf.urls import patterns, url
from .views import ActiveTabTemplateView, GroupOrTeamCreate, GroupOrTeamUpdate
urlpatterns = patterns('',
# Add page
url(r'^add/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'), name='add_page'),
url(r'^add/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'),
name='add_page'),
# Seach page
url(r'^search/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'), name='search_page'),
url(r'^search/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'),
name='search_page'),
#--------------------------------------------------------------------------
# Forms
#--------------------------------------------------------------------------
# Groups and teams
url(r'^group-or-team-form/$', GroupOrTeamCreate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
url(r'^group-or-team-form/(?P<pk>\d+)/$',
GroupOrTeamUpdate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
)
I'm not seeing the problem.. these pages load just fine without that reverse statement in, it appears to be the introduction of the reverse statement that breaks everything but I can't for the life of me work out what the cause is.

You get the error because the URL conf has not been loaded yet when the class is defined.
Use reverse_lazy instead of reverse.
from django.core.urlresolvers import reverse_lazy
success_url = reverse_lazy('home_page')

Related

Python - Django add multiple urlpatterns for multiple views of template

I'm very very new to Python 3 and Django and I get to the following problem: I use a standard Template and now how to set it up when there is 1 view. But I don't get the code right for multiple views. I currently run the page locally
At the moment I have tried to change different orders within urlpatterns, and they do work when only 1 url in in there, but I can't get the second one in
views.py
from django.shortcuts import render, render_to_response
# Create your views here.
def index(request):
return render_to_response('index.html')
def store(request):
return render_to_response('store.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^store/$', views.store, name='store'),
url(r'^admin/', admin.site.urls)
]
urlpatterns += staticfiles_urlpatterns()
I would like the url pattern that lets me go to the index view and the store view
EDIT:
Full code is shared via: https://github.com/lotwij/DjangoTemplate
The error in the comments shows you are going to http:/127.0.0.1:8000/store.html, but your URL pattern url(r'^store/$', ...) does not include the .html, so you should go to http:/127.0.0.1:8000/store/.
The Django URL system uncouples the URL from the name of the template (sometimes the view doesn't even render a template!). You could change the regex to r'^store.html$ if you really want .html in the URL, but I find the URL without the extension is cleaner.

Django Oscar change URL pattern

I have setup a django-oscar project and I'm trying to configure the URLs. My goal is to change /catalogue to /catalog.
As per the documentation I've added app.py in myproject/app.py
myproject/app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urlpatterns = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
return urlpatterns
application = MyShop()
myproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from .app import application
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
url(r'^index/$',views.index, name = 'index'),
]
The project server runs without any error, but when I try localhost:8000/catalog I get
NoReverseMatch at /catalog/ 'customer' is not a registered namespace.
The expected output is localhost:8000/catalog should return the catalogue page.
You can try this
in app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urls = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
urls = urls + super(MyShop,self).get_urls()
return urls
application = MyShop()
And in your urls.py
you can simply add this
from myproject.app import application as shop
url(r'', shop.urls),
Hope it help for you
Expanding on c.grey's answer to specify how to replace instead of add the urls -
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
def get_urls(self):
urls = super(MyShop, self).get_urls()
for index, u in enumerate(urls):
if u.regex.pattern == r'^catalogue/':
urls[index] = url(r'^catalog/', include(self.catalogue_app.urls))
break
return urls
application = MyShop()
You need to include the URLs, not reference them directly.
url(r'', include('application.urls')),

View function is not called from urls.patterns

My view's function are not called by urls.py patterns. I can only call them explicitly.
Basic Layout is
--project
----persons
----project
project/urls.py is:
from django.conf.urls import url, include, patterns
from .views import page #irrelevant
# from persons import views as person_views
urlpatterns = patterns('',
url(r'^(?P<slug>[\w./-]+)/$', page, name='page'), #irrelevant
url(r'^$', page, name='homepage'), #irrelevant
url(r'^persons/', include('persons.urls', namespace='persons')), # WORKS
# url(r'^persons/$', person_views.persons, name='persons'), #wont work
# url(r'^persons/add/$', person_views.add_person, name='add_person'), #wont work
)
Everything is ok until this point, since persons.urls is included successfully... But inside:
persons/urls.py:
from django.conf.urls import patterns, url
#from persons.views import index_persons, add_person
from persons import views
#views.index_persons('GET / HTTP/1.0') # >>> WORKS - function called <<< !!!
urlpatterns = patterns('',
url(r'.', views.index_persons, name='index_persons'), # DOES NOT WORK
url(r'^add/', views.add_person, name='add_person'), # DOES NOT WORK
)
I have also tried other regex like:
url(r'*', views.index_persons, name='index_persons'), # DOES NOT WORK
url(r'^$', views.index_persons, name='index_persons'), # DOES NOT WORK
no luck...
My persons/views.py file contains:
def index_persons(request):
print 'WHY???'
def add_person(request):
print 'WHY???'
'WHY???' is normally printed in the console (stdout - since I execute from manage.py runserver), when the index_persons function is called explicitly from persons/urls.py
Any thoughts?
In project/urls.py, move the page url pattern below the other ones. Otherwise, a request to /persons/ will be matched by the page url pattern first.
url(r'^$', page, name='homepage'),
url(r'^persons/', include('persons.urls', namespace='persons')),
url(r'^(?P<slug>[\w./-]+)/$', page, name='page'),
Inside persons/urls.py, you should have:
urlpatterns = patterns('',
url(r'^$', views.index_persons, name='index_persons'),
url(r'^add/$', views.add_person, name='add_person'),
)
Your url rule should be
url(r'^/?$, views.index_persons, name='index_persons'),
NOTE 1: Don't forget to restart the server.
NOTE 2: namespace='persons' is equal to not set namespace, because your url is persons/ is the same.

Create 404 with Django 1.4

I would like to show an 404 error if there's no proper entry in project_name/urls.py.
I read Writing your first Django app but i don't understood how to create a general error - not only for an app.
So i tried this:
TEMPLATE_DIRS = /var/www/first/templates
echo "404 ERROR" > /var/www/first/templates/404.html
in /var/www/first/urls.py:
from django.http import Http404
try:
...
url(r'^admin/', include(admin.site.urls)),
...
except:
raise Http404
After all i still get a 500 error if i call an unknown url.
Django uses the urls.py file to evaluate all the url patterns by order. If you want it to go to a certain page after it evaluated all the regex patterns and found no match, just add something like this as the last item in the urlpatterns tuple:
url('', views.defaultview),
with defaultview possibly being that 404 page.
django.views.defaults.page_not_found is a built-in django view that renders whatever's in templates/404.html.
It works with following in /var/www/first/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
...,
url(r'^admin/', include(admin.site.urls)),
...,
)

The included urlconf manager.urls doesn't have any patterns in it

A solution: Found the following django snippet that seems to work fine
(http://djangosnippets.org/snippets/2445/)
from django.utils.functional import lazy
from django.core.urlresolvers import reverse
#Workaround for using reverse with success_url in class based generic views
#because direct usage of it throws an exception.
reverse_lazy = lambda name=None, *args : lazy(reverse, str)(name, args=args)
Apparently, there is now a reverse_lazy function in django trunk.
Update: This error has something to do with me making a call to reverse inside a generic view:
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
success_url = reverse('manager-personal_objs')
Is this not valid?
If I instead of generic write something like this, it works:
def add_obj(request, pk):
a=reverse('manager-personal-objs')
return HttpResponse(a)
I have a project with 2 apps in it. Each app has its urls and views. They both work fine, but on the manager app, as soon as I reference the reverse function in the views(any view), I get the following error:
Exception Type: ImproperlyConfigured
Exception Value: The included urlconf manager.urls doesn't have any patterns in it
The urls file:
urlpatterns = patterns('',
url(r'^$', ObjView.as_view(), name='manager-obj'),
url(r'^add/$', AddObjView.as_view(), name='manager-add_obj'),
url(r'^personal/$', PersonalObjsView.as_view(), name='manager-personal_objs'),
)
Exception Location: ...site-packages\django\core\urlresolvers.py in _get_url_patterns, line 283
I get this error in the entire site(edit: this apparently happens because an attempt to import the manager.urls will result in the error). If I remove the include manager.urls, everything goes back to work; if I remove the call to reverse, everything is fine; if I try to rewrite manager.urls to a simpler version, it continues with the error.
I've been over this many times, can't seem to find anything wrong.
edit:root urls.py
# coding=utf8
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.simple import direct_to_template
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# Home Page
url(r'^$', direct_to_template, {'template': 'home.html'}, name="home"),
# manager
url(r'^manager/', include('manager.urls')),
# writing
url(r'^writing/', include('writing.urls')),
)
urlpatterns += staticfiles_urlpatterns()
edit2: Should also be noted that the url template tag works fine in the manager app and the reverse call works if I do it on the other app.
Also, every url has a written working view.
The problem is that your URLConf hasn't finished loading before your class based view attempts to reverse the URL (AddObjView.success_url). You have two options if you want to continue using reverse in your class based views:
a) You can create a get_success_url() method to your class and do the reverse from there
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
def get_success_url():
return reverse('manager-personal_objs')
b) If you are running on the trunk/dev version of Django, then you can use reverse_lazy https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy
from django.core.urlresolvers import reverse_lazy
class AddObjView(CreateView):
form_class = ObjForm
template_name = 'manager/obj_add.html'
success_url = reverse_lazy('manager-personal_objs')
Option "b" is the preferred method of doing this in future versions of Django.