Django NoReverseMatch exception with cache_page in urls.py - python

Question
Why does using the cache_page function in urls.py under Django 1.4 cause a NoReverseMatch error when using the url tag in a template?
Setup
views.py
from django.shortcuts import render
def index(request):
'''Display the home page'''
return render(request, 'index.html')
urls.py
Following the cache_page directions:
from django.conf.urls import patterns, include, url
from django.views.decorators.cache import cache_page
from my_project.my_app import views
urlpatterns = patterns('',
url(r'^$', cache_page(60 * 60)(views.index)),
)
index.html
{% url my_project.my_app.views.index %}
Error Message
NoReverseMatch at /
Reverse for 'my_project.my_app.views.index' with arguments '()' and keyword arguments '{}' not found.
The offending line the error message points out is, of course:
{% url my_project.my_app.views.index %}
What I've tried so far
A ton of googling and searching on SO
Simplifying code down to the example above to rule out other conflicts
Used cache_page in views.py as a decorator successfully (not recommended according to docs)
Offerings to our all-powerful Django overlords

When doing reverse, Django tries to match by
URL label
dotted path
callable
In your code, 'my_project.my_app.views.index' is dotted path, then Django would get the actual function index() and use it as the key to match the reversed URL, by looking up in the mapping dictionary django.core.urlresolvers.get_resolver(None).reverse_dict.
However, when you wrap the view.index by cache_view, the key in the mapping dictionay becomes the wrapper. Thus the lookup fails and NoReverseMatch is raised. This is inconvenient and error-prone, but I'm not sure whether it is a bug.
You could then solve this by either use URL label
url(r'^$', cache_page(60 * 60)(views.index), name='my_index'),
{# in template #}
{% url my_index %}
or used cache_page in views.py as a decorator as you mentioned.

Related

Error during template rendering?

I have a blog app, this is the relevant part of views.py, of course belonging to the blog app.
from django.conf import settings
from django.shortcuts import render
from .models import Blog
# Create your views here.
def view_homepage(request):
return render(request, 'index.html', {})
def view_aboutpage(request):
return render(request, 'about.html', {})
def view_blogpost(request, blog_id):
article = Blog.objects.get(pk=blog_id)
return render(request, 'damn.html', {'article':article})
this is the urls.py in blog app
from django.conf.urls import url, include, patterns
from blog import views
urlpatterns = [
url(r'^$', views.view_homepage, name=''),
url(r'about/$', views.view_aboutpage, name='about'),
url(r'blog/(?P<blog_id>\d+)/$', views.view_blogpost, name='post'),
]
This is the regular urls.py in the project.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls')),
]
This is the error, I am having.
NoReverseMatch at /
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Exception Location: C:\Users\filip\Python\ENV\lib\site- packages\django\core\urlresolvers.py in _reverse_with_prefix, line 508
Python Executable: C:\Users\filip\Python\ENV\Scripts\python.exe
Python Version: 3.5.1
This is the html:
<li>
Home
</li>
<li>
About
</li>
<li>
Sample Post
</li>
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P\d+)/$']
Like the error says, you don't have a URL called "post" that takes no arguments; the URL you do have called that is expecting a blog_id argument. So, you should pass that in your tag; since you have a context variable called article it would be:
Sample Post

Django Haystack Reverse URL Failing

I am currently trying to configure the most barebones possible setup for Django Haystack. In my HTML, I have a form that resolves a url using a named url pattern. Here is the HTML code.
<form id="search-ticket-form" class="navbar-form navbar-left dropdown" method="get"
action="{% url "search_ticket" %}" role="search">
Django returns an error every time saying "Reverse for 'search_ticket' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
Here is the configuration in my urls.py:
urlpatterns = patterns('',
url(r'^$', contact.views.home, name='homepage'),
#url(r'^blog/', include('zinnia.urls', namespace='zinnia')),
url(r'^profile/', include('user_profile.urls')),
url(r'^registration/', include('registration.urls')),
url(r'^comments/', include('django_comments.urls')),
url(r'^contact/', include('contact.urls')),
url(r'^tickets/', include('tickets.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('haystack.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is the configuration in my tickets/urls.py:
urlpatterns = patterns('',
url(r'submit_ticket/$', submit_ticket, name='submit_ticket'),
url(r'search_ticket/$', include('haystack.urls'), name='search_ticket')
)
The setup certainly looks fine. When I substitute include('haystack.urls') for a function based view named 'abc', the url resolves just fine. This makes me think that something is wrong with my Django Haystack setup, but the error certainly is misleading. Here is what my single Haystack view looks like:
class TicketIndex(indexes.BasicSearchIndex, indexes.Indexable):
def get_model(self):
return Ticket
I modeled this setup after the barebones example in Haystack's github repo (https://github.com/toastdriven/django-haystack/blob/master/example_project/bare_bones_app/search_indexes.py).
Any thoughts on what is going on here?
Can you actually name an included URL tree? It usually includes multiple URL patterns. Looking at haystack.urls you may want to try {% url "haystack_search"%}.

NoReverseMatch error when using Django url template tag with optional parameters, but NOT when visiting the URL in the browser

I have a URL that needs to account for the following patterns:
localhost:8000/staffing-agencies
localhost:8000/staffing-agencies/90210 (zip code)
localhost:8000/staffing-agencies/portland-or (city-state)
When I type in any of these urls in my browser, they all work as expected. However, I'm getting a NoReverseMatch error when I try to refer to this URL from Django's url template tag.
Here are my relevant url.py files:
# From urls.py
urlpatterns = patterns('',
url(r'^', include('bos.apps.search.urls', namespace='search',
app_name='search')),
)
# From search/urls.py
urlpatterns = patterns('bos.apps.search',
url(r'^staffing-agencies/'
r'((?P<city>[a-zA-Z]+)-(?P<state>[a-zA-Z]{2}))?'
r'((?P<zip>[0-9]{5}))?$',
'views.main', name='main'),
)
I thought it might be something to do with the optional parameters, but all of these variances throw the NoReverseMatch error:
Test
Test
This variance does NOT throw an error:
Test
I'm using Django 1.6.5
This not better solution, this is one of solution
url(r'^staffing-agencies/(?P<city>[a-zA-Z]+)*-(?P<state>[a-zA-Z]{2})*?(?P<zip>[0-9]{5})*?$',
'views.main', name='main'),
in views:
def main(request, city=None, state=None, zip=None):
in html:
Test
In this case url work like this,
localhost:8000/staffing-agencies
localhost:8000/staffing-agencies/-90210 (zip code)
localhost:8000/staffing-agencies/portland-or (city-state)

Understanding django.shortcuts.redirect

I have a couple problems understanding how redirect or rather reverse really work.
In the main urls.py I have:
from django.conf.urls import patterns, include, url
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
url(r'^$', redirect_to, {'url': '/monitor/'}),
url(r'^monitor/', include('monitor.urls')),
)
and in monitors.urls I have:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('monitor.views',
(r'^$', 'index'),
(r'^abc/(?P<id>.*$)', 'abc'),
)
When you call /monitor I want to redirect it to /monitor/abc so I did:
def index(request):
return redirect("abc")
def abc(render, id=None):
return render_to_response("monitor/list.htmld", {})
But I got an NoReverseMatch exception. But when I do:
def index(request):
return redirect("abc/")
then it suddenly works.
I cannot fully understand why. Why did reverse fail with abc but not with abc/? And how does reverse know that the redirect should include monitor/ as well? What if I had in the main urls.py another app called xyz which also has a abc view?
Why did reverse fail with 'abc' but not with 'abc/'?
Because it interpreted it as a view name (and you indeed have a view named 'abc', see your monitor.urls file). This means Django will call reverse to compute the URL. The value abc/ is interpreted as an actual URL which means Django won't call reverse to determine the URL.
This also explains why reverse failed: the view with name abc also requires an argument called id. Otherwise Django won't be able to lookup the URL as there is no view called abc without parameters.
Based on the documentation you should be able to reverse the URL using:
redirect("abc", id=...)
where ... is the value of the id parameter.
And how does reverse know that the redirect should include monitor/ as well?
That is because it knows what URLs are available and 1) it knows where the view called abc is defined and 2) it knows that monitors.urls is included with monitor/ in front.
What if I had in the main urls.py another app called "xyz" which also has a "abc" view?
In that case you have to use namespaces.

Django reverse url with parameters to a class based view

I just started learning python and django and I have a question.
I got the assignment to turn function views into class based views. But my links wont work now.
these are from urls.py:
url(r'^$', ContactIndex.as_view()),
url(r'^add$', ContactAdd.as_view()),
url(r'^([0-9]+)/update$', ContactUpdate.as_view()),
url(r'^([0-9]+)/view$', ContactView.as_view()),
This is my link :
{% url rtr_contact.views.ContactView contact.id %}
but this doesnt work it says:
Caught NoReverseMatch while rendering: Reverse for 'rtr_contact.views.ContactView' with arguments '(20L,)' and keyword arguments '{}' not found.
To make url reversing easy, I recommend that you always name your url patterns.
url(r'^$', ContactIndex.as_view(), name="contact_index"),
url(r'^add$', ContactAdd.as_view(), name="contact_add"),
url(r'^([0-9]+)/update$', ContactUpdate.as_view(), name="contact_update"),
url(r'^([0-9]+)/view$', ContactView.as_view(), name="contact_view"),
Then in the template:
{% url contact_view contact.id %}

Categories