I have in app_main/templates/app/detail.html:
{% for i in user_items %}
<li>{{ i }}</li>
{% endfor %}
I have in app_main/urls.py:
url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$', views.update_last_clicked, name='update_last_clicked'),
And I have in app_main/views.py:
def update_last_clicked(request, username, url):
user = get_object_or_404(User, username=username)
user.item_set.get(url=url).last_clicked += 1
return HttpResponseRedirect(url)
I get the NoReverseMatch Error as follows:
NoReverseMatch at /app_main/u/florin/
Reverse for 'update_last_clicked' with keyword arguments '{u'username': u'florin', u'url': u'fg'}' not found. 1 pattern(s) tried: [u'app_main/u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$']
Request Method: GET
Request URL: http://127.0.0.1:8080/app_main/u/florin/
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'update_last_clicked' with keyword arguments '{u'username': u'florin', u'url': u'fg'}' not found. 1 pattern(s) tried: [u'app_main/u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$']
Exception Location: /Users/f/Desktop/django/lib/python2.7/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable: /Users/f/Desktop/django/bin/python
Python Version: 2.7.10
With more:
In template /Users/f/Desktop/django/app/app_main/templates/app_main/detail.html, error at line 5
Reverse for 'update_last_clicked' with keyword arguments '{u'username': u'florin', u'url': u'fg'}' not found. 1 pattern(s) tried: [u'app_main/u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$']
I don't understand what's going on: I just want to update an attribute in the item object that is clicked, but with this code added, it gives me this error when I just go to the detail.html view, whose urlpattern is url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/$', views.detail, name='detail'),
The complete urlpatterns in app_main/urls.py is:
app_name = 'app_main'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/$', views.detail, name='detail'),
url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/add_item/$', views.add_item, name='add_item'),
url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$', views.update_last_clicked, name='update_last_clicked'),
]
For reference, app/urls.py has url(r'^app_main/', include('app_main.urls')),
I essentially want to pass the item clicked to a view without a form. I figure since I just need to pass the item's url attribute, I can append it to the current url to pass it. Am I wrong? Do I have to use an AJAX call or a form?
FIX: I kept changing things and what worked was just changing url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>[.]*)/$', views.update_last_clicked, name='update_last_clicked'), to url(r'^u/(?P<username>[a-zA-Z0-9_.-]+)/update_last_clicked/(?P<url>.*)/$', views.update_last_clicked, name='update_last_clicked'),. Does anyone understand why simply taking out the grouping brackets changes this?
You have to give a namespace to the app.
For eg -
url(r'^app_main/', include('app_main.urls', namespace='app_main'))
Similary, for all other apps for which you are going to reverse based on app and url name.
You can also do this by mentioning the app_name in app_main/urls.py like this -
app_name = 'app_main'
urlpatterns = [...]
Ref: https://docs.djangoproject.com/en/1.11/topics/http/urls/#reversing-namespaced-urls
Related
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
After looking through pages of similar answers, I regret to say that I'm still stumped. I'm pretty sure it's an error in the urlconf but anywho, here's all the relative info:
URLCONF in APP
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tenants/$', views.tenant_index, name = 'tenant_index'),
url(r'^(?P<property_alias>[\w\-]+)/$', views.property_detail, name='property_detail'),
url(r'^tenants/(<?P<first>\w+)/(<?P<last>)\w+/$', views.tenant_detail, name = 'tenant_detail'),)
index.html
<h1> List of Tenants</h1>
{% for tenant in tenants %}
<ul> {{ tenant}} </ul>
<h2> details </h2>
{% endfor %}
segment of views.py
def tenant_detail(request, first, last):
tenant = Tenant.objects.filter(first_name__startswith = first,
last_name__startswith = last)
tenant = get_object_or_404(Tenant, pk=tenant[0].pk)
return render(request, 'my_properties/tenants/tenant_detail.html', {'tenant': tenant})
the error itself is:
NoReverseMatch at /properties/tenants/
Reverse for 'tenant_detail' with arguments '()' and keyword arguments '{u'last': u'no', u'first': u'yay'}' not found. 1 pattern(s) tried: ['properties/tenants/(<?P<first>\\[0-9A-Za-z._%+-]+)/(<?P<last>)\\[0-9A-Za-z._%+-]+/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/properties/tenants/
Django Version: 1.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'tenant_detail' with arguments '()' and keyword arguments '{u'last': u'no', u'first': u'yay'}' not found. 1 pattern(s) tried: ['properties/tenants/(<?P<first>\\[0-9A-Za-z._%+-]+)/(<?P<last>)\\[0-9A-Za-z._%+-]+/$']
Does anyone know what's wrong? Seems like I'm following the correct general procedure
The error is in your regexp.
The \w+ of the 'last' group was outside the bracket, and there is a typo on the named group syntax.
This should work: r'^tenants/(?P<first>\w+)/(?P<last>\w+)/$'
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.
I am trying to use the {% url %} tag to send my user to a new template, called payment.html. I am receiving the error above. Here's what I have:
In some Javascript in my template:
{% url 'payment' %}
In my urls:
urlpatterns = patterns('',
(r'media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^pc-admin/', include(pcAdminSite.urls)),
(r'^$', index),
(r'^signup/', signup),
(r'^info_submit/', info_submit),
(r'^payment/', payment, name="payment"),
In my views:
def payment(request):
return render_to_response('payment.html', {})
Based on similar posts, I've made sure that only 1 URL points to my payment view and have used named URLs. Can anyone help with what I might be doing wrong? Thanks.
There shouldn't be any '' surrounding the view name in the url tag - check the documentation out.
(Unless you're using the Django's 1.5 future {% url %} tag.)
this error also occurs this way :
My account
the message will be this one
Reverse for ''profiles_profile_detail'' with arguments '(u'mynick',)' and keyword arguments '{}' not found.
once the two '' dropped like explain erlier everything works fine ;)
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 %}