NoReverseMatch with Django-survey? - django

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

Related

django url tag generating NoReverseMatch

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.

How to pass an optional argument to url in Django

I have an url pattern with one optional parameter:
# urls.py :
url(r'^(page/(?P<page>\w+))?$', MyIndexView.as_view(), name='index'),
Pagination and everything else works well, until I create an url to a specific page in my template:
# templates/mysite.html
{% url 'index' 54 %}
Then I get an error:
Reverse for 'index' with arguments '(54,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'(page/(?P<page>\\w+))?$']
Without the parameter it works:
{% url 'index' %}
I also tried:
{% url 'index' page=54 %}
and got similar error.
You can create two URL patterns, one with a default parameter to 1 and the other with the page matching in the URL:
# urls.py :
url(r'^page$', MyIndexView.as_view(), {'page': 1}, name='index'),
url(r'^page/(?P<page>\w+)$', MyIndexView.as_view(), name='index'),
The third argument of the url function is kwargs, hence kwargs['page'] will be 1 in the first case and defined by the URL in the second.

How can I get {% url logout %} to work in my Django template?

My Django app's site-wide urls.py file looks like this:
urlpatterns = patterns('',
url(r'^$', include('myApp.urls')),
url(r'^admin/', include(admin.site.urls)),
url (
r'^accounts/register/$',
RegistrationView.as_view(form_class=extendedRegistrationForm),
),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^', include('myApp.urls')),
)
I also have a urls.py specific to myApp but I have not shown that here because I don't think it's relevant.
In my template file, I have this:
{% url logout %}
It gives me this error:
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
When I change the template to:
{% url 'logout' %}
It gives me the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How do I put a link to logout in my view?
Add a logout url like this:
url(r"^logout/$", "django.contrib.auth.views.logout_then_login",
name="logout"),
The correct template syntax is:
{% url 'logout' %}
You didn't include django.contrib.auth's URLs in your urls.py, so there is no logout URL.
Try adding something like
url(r'^auth/', include('django.contrib.auth.urls')),
In django 3.0 {% url 'admin:logout' %} works perfect.

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" %}

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