django url tag generating NoReverseMatch - django

When using the django url tag with a dynamic parameter in a Django template, the following snippet:
<ul>
{% for m in markets %}
<li>{{ m.mic }}</li>
{% endfor %}
</ul>
is always generating a NoReverseMatch exception:
Error during template rendering
In template D:\X\django\project0\core\templates\markets\index.html, error at line 29
Reverse for 'market_detail' with keyword arguments '{'market_mic': ''}' not found. 1 pattern(s) tried: ['core/market/(?P<market_mic>[^/]+)/$']
It is configured like this in core/urls.py:
from django.urls import path
from . import views
app_name = 'core'
urlpatterns = [
# ex: /core/market/
path('market/', views.market_index, name='market_index'),
# ex: /core/market/XPAR/
path('market/<str:market_mic>/', views.market_detail, name='market_detail'),
# ex: /core/market/XPAR/all
path('market/<str:market_mic>/all', views.market_all_udls, name='market_all_udls'),
When trying to get the reverse with the shell (python manage.py shell), it works:
In [5]: reverse('core:market_detail', kwargs={'market_mic': 'XPAR'})
Out[5]: '/core/market/XPAR/'
It is likely Django is unable to resolve the market variable in the url tag while elsewhere it is rendering it as expected.

Related

Django : No Reverse Match on password reset [duplicate]

This question already has answers here:
What is a NoReverseMatch error, and how do I fix it?
(6 answers)
NoReverseMatch exception while resetting password in django using django's default views
(1 answer)
Closed 5 years ago.
I am working on password reset functionality in Django using custom templates.
But when I entered mail:id and clicking on reset password I'm facing "No Reverse match error".Please someone help me,I understand this is an old problem, I have followed other answers and tried solving but could not come up with solution.Please find the code below and help me solve this problem.
urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from django.contrib.auth.views import password_reset,password_reset_done,password_reset_confirm,password_reset_complete
from surveysite.views import home,about,login,loggedin,loggedout
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^about/$',about, name='about'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^accounts/login/$', login, name='login'),
url(r'^accounts/loggedin/$',loggedin, name='loggedin'),
url(r'^accounts/logout/$', loggedout, name='logout'),
url(r'^accounts/password_reset/$', password_reset,
{'post_reset_redirect' : '/accounts/password_reset/mailed/'},
name="password_reset"),
url(r'^accounts/password_reset/mailed/$',password_reset_done),
url(r'^accounts/password_reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',password_reset_confirm,
{'post_reset_redirect' : '/accounts/password_reset/complete/'},name="password_reset_confirm"),
url(r'^accounts/password_reset/complete/$',password_reset_complete),
url(r'^admin/', admin.site.urls),
]
Password_reset_email
{% autoescape off %}
You're receiving this email because you requested a password reset for your user account at {{ site_name }}.
Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %}
{% endblock %}
Your username, in case you've forgotten: {{ user.get_username }}
Thanks for using our site!
The {{ site_name }} team
{% endautoescape %}
Error Stack Trace
NoReverseMatch at /accounts/password_reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://localhost:8000/accounts/password_reset/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python27\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable: C:\Python27\python.exe
Python Version: 2.7.13
Python Path:
['G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\Python27',
'C:\\Python27\\Tools\\Scripts',
'G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\WINDOWS\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
In template G:\Manoj\Software Engineering-Yui Man lee\Django_Project\surveysite\templates\registration\password_reset_email.html, error at line 6
I have tried solutions from below links but could not find the solution.
NoReverseMatch on password_Reset_confirm
https://github.com/bread-and-pepper/django-userena/issues/380
django password reset NoReverseMatch error
I'm following tutorial from http://code.techandstartup.com/django/reset-password/ ==> customized Templates.
Instead of referencing the view, you need to just reference the name of your url pattern that you are trying to match
{% url 'password_reset_confirm' uidb36=uid token=token %}

NoReverseMatch in render_to_response with django-social-auth

I would like to make just a page that has link to login to twitter/facebook/google with django-social-auth.
but I get a error NoReverseMatch: Reverse for '' with arguments '(u'twitter',)' and keyword arguments '{}' not found.
def index(request):
ctx = {}
return render_to_response('index_before_login.html', {}, RequestContext(request))
index_before_login.html is following
<li>Enter using Twitter</li>
urls.py is following
urlpatterns = patterns('',
url(r'^$', 'lebabcartoon.views.index'),
#url(r'^socialauth_', 'lebabcartoon.views.index'),
url('', include('social_auth.urls')),
my environment is
Django ver1.5
Python Version: 2.7.3
django-social-auth: 0.7.5
anyideas?
Wrap url name in quotes
{% url 'socialauth_begin' 'twitter' %}
To save someone using the new python-social-auth and django > 1.4
Use this :
{% url 'social:begin' 'twitter' %}
I had a similar problem, try adding the following to the url.py file in your project.
url(r'auth/', include('social_auth.urls'))
And also make sure your url parameters are wrapped in quotes like so.
{% url "socialauth_begin" "twitter" %}

Django NoReverseMatch

I have the following setup:
/landing_pages
views.py
urls.py
In urls.py I have the following which works when I try to access /competition:
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"),
)
My views.py has something like this:
def page(request, page_name):
return HttpResponse('ok')
Then in a template I'm trying to do this:
{% load url from future %}
<a href="{% url 'landing_pages.views.page' page_name='competition'%}">
Competition
</a>
Which I apparently can't do:
Caught NoReverseMatch while rendering: Reverse for 'landing_pages.views.page' with arguments '()' and keyword arguments '{'page_name': u'competition'}' not found.
What am I doing wrong?
You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?
If you want to allow the user to specify arguments to your view, you have to provide a URL pattern with a space for those arguments to go.
{% url [project_name].landing_pages.views.page page_name='competition' %}
Or better
{% url competition_landing 'competition' %}

NoReverseMatch with Django-survey?

Ok, so this works in my urls.py:
url(r'^', survey_detail, {'survey_slug':'my-survey'}, name='survey-detail'),
I removed the above line and want to put the link in a template using {% url %}
I have the following line in urls.py:
url(r'^survey/', include('survey.urls'),
This is the line from the urls.py of the Survey app:
url(r'^detail/(?P<survey_slug>[-\w]+)/$', survey_detail, name='survey-detail'),
And this in my template:
{% url survey_detail 'my-survey' %}
I have also tried:
{% url survey_detail survey_slug='my-survey' %}
But I always get:
Reverse for 'survey_detail' with arguments '(u'my-survey',)' and keyword arguments '{}' not found.
or
Reverse for 'survey_detail' with arguments '()' and keyword arguments '{'survey_slug': u'my-survey'}' not found.
Any ideas?
The name is survey-detail and not survey_detail

NoReverseMatch Error

I keep getting this error for the django login system. Here is part of my urls.py:
(r'^contractManagement/login', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
The exact error I am getting:
Exception Type: NoReverseMatch
Exception Value: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.
I can't understand why i am getting this error. If you need anything else let me know.
You don't show where you are trying to reverse this URL, but it looks like you have double-quoted it. If you're using the url tag, note that you don't need quotes around the url name:
{% url django.contrib.auth.views.login %}
not
{% url 'django.contrib.auth.views.login' %}
You see that ''the.unknown.view'' is reported including too many qoutes.
It is because the quoted syntax will be valid in Django 1.5 and higher. For Django 1.3 or 1.4, you should activate the future behavior by this line in the template:
{% load url from future %}
which is valid also for Django 1.5.
Example for Django 1.5+
{% url "path.to.some.view" %}
Classic syntax for Django <= 1.4.x (without "future" command) is:
{% url path.to.some.view %}
I would give your url a name (in order to do that, you need to use the url method) Also you should add a trailing slash to all your urls, cause the django CommonMiddleware is going to be doing a 302 redirect on all your urls if you don't:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^contractManagement/login/', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='contract_login'),
)
Then you can use reverse in your code, or url in your templates, and if you ever decide to change the actual url (ie: changedCotractManagement/login/), as long as the name is the same, your code will still be good.
in code:
from django.core.urlresolvers import reverse
reverse('contract_login')
in template:
{% url contract_login %}
Edit: per MrOodles