"NoReverseMatch at /" when trying to create a link in template - python

I'm following the Django tutorial, and in the third part, they create a link in the template. Now, I followed and wrote a template like this:
<ul>
{% for blog in blogs %}
<li>
<a href="{% url 'detail' blog.id %}">
<h1>{{ blog.name }}</h1>
</a>
<p>{{ blog.description }}</p>
</li>
{% endfor %}
</ul>
And I have set the urls.py like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<blog_id>[0-9]+)/$', views.detail, name='detail'),
]
However, when I point my browser to the index, it gives me the following error:
NoReverseMatch at /
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
1 pattern(s) tried: ['$(?P<blog_id>[0-9]+)/$']
The error details highlight this part of the template:
<a href="{% url "detail" blog.id %}">
What's going on in here? How do I fix this? I'm using Django 1.8.4.

You are mispelled in your main urls.py, something like.
url(r'^polls/$', include('polls.urls')),
You have to write like this
r'^polls/'. # Note please remove `$` in end

Related

Reverse for 'path' with no arguments not found. 1 pattern(s) tried [duplicate]

This question already has an answer here:
Unexpected NoReverseMatch error when using include() in urls patterns
(1 answer)
Closed 5 years ago.
So I have recently started learning Django, after finishing the tutorial I decided to create my own website and have run into an error that I honestly just don't understand.
The error is:
Error during template rendering
In template /home/phil/Projects/mysite/homepage/templates/_partial.html,
error at line 13
Reverse for 'bio' with no arguments not found. 1 pattern(s) tried:
['$bio/']
homepage/templates/_partial.html
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="{% url 'home:index'
%}">Home <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="{% url 'home:bio' %}">Bio</a> <!--Attention-->
<a class="nav-item nav-link disabled" href="{% url 'home:index'
%}">Technologies</a>
</div>
</div>
homepage/url.py
from django.views import generic
class IndexView(generic.ListView) :
template_name = 'homepage/index.html'
class BioView(generic.ListView) :
template_name = 'homepage/bio.html'
homepage/urls.py
from django.conf.urls import url
from . import views
app_name = 'home'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^bio/', views.BioView.as_view(), name='bio'),
url(r'^skills/', views.SkillsView.as_view(), name='skills'),
]
app/urls.py
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('homepage.urls')),
url(r'^home/$', include('homepage.urls')),
url(r'^admin/', admin.site.urls),
]
As I said I had followed the Django tutorial and honestly can't see what the error is.
Index works as expected, but when I call bio or skills it throws this exception.
Edit: adding requested views.
:(
If you use a name, you can call that name directly in the url, so:
{% url bio %}
Instead of:
{% url name:bio %}
Here's a link: https://docs.djangoproject.com/en/1.11/topics/http/urls/#url-namespaces-and-included-urlconfs.

multiple django urls arguments not working

urls.py
urlpatterns = [
url(r'^$', views.gallery, name='home'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
url(r'^(?P<album_id>\d+)/', views.album_details, name='album_details'),
]
views.py
def details(request, pic_id):
picture = get_object_or_404(Picture, pk=pic_id)
print("accessed details %s" %picture)
context = {
"picture": picture
}
return render(request, "picture_details.html", context)
gallery_details.html
{% for picture in pictures %}
<div class="img">
<a href="{% url 'gallery:details' picture.id %}">
<img src="{{ picture.picture_thumbnail.url }}" />
</a>
<div class="desc">{{ picture.description|truncatewords:5 }}</div>
</div>
{% endfor %}
When i try to run this i get an exception value:
Reverse for 'details' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['gallery/(?P<album_id>\\d+)/(?P<pic_id>\\d+)/$']
It should load page with single image, but it doesn't. Don't know why.
You may want to rearrange your urls so that the less specific one is first
url(r'^(?P<album_id>\d+)$', views.album_details, name='album_details'),
url(r'^(?P<album_id>\d+)/(?P<pic_id>\d+)/$', views.details, name='details'),
You will also note that I've modified the first regex to include the $ character to indicate thats the end of the match

Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found

Background
I am trying to customize the authentication views in a Django project, but I can't seem to get the customized password_change view to run. I use Django 1.8.2 and Python 2.7.
The urls.py of my module userauth looks like the following:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('django.contrib.auth.views',
url(r'^login/$', 'login', {'template_name': 'userauth/login.html'},
name='userauth_login'),
url(r'^logout/$', 'logout', {'next_page': '/'},
name='userauth_logout'),
url(r'^password-change/$', 'password_change',
{'template_name': 'userauth/password_change_form.html'},
name='userauth_password_change'),
url(r'^password-change-done/$', 'password_change_done',
{'template_name': 'userauth/password_change_done.html'},
name='userauth_password_change_done'),
)
this is referenced in the main urls.py as this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('userauth.urls')),
]
The template of my userauth/password_change_form.html
{% extends "base.html" %}
{% block title %}{{ block.super }} - Change Password{% endblock %}
{% block toggle_login %}{% endblock %}
{% block content %}
<form action="{% url 'userauth_password_change' %}" method="post" accept-charset="utf-8">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="Change password"/>
</form>
{% endblock %}
And the template for userauth/password_change_done.html
{% extends "base.html" %}
{% block title %}{{ block.super }} - Password change successful{% endblock %}
{% block content %}
<p>Your password has been changed successfully.</p>
Back to your Account
{% endblock %}
The Problem
When I open the 'password_change_done' page (at /account/password-change-done), then everything is fine.
But at 'password-change' (/accunt/password-change) I am getting this error:
NoReverseMatch at /account/password-change/
Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
What I tried
I have no idea, why this should be happening.
I tried removing the single quotes from url 'userauth_password_change'
I made sure the password-change-donepage exists in urls.py and is available
I read the solutions at Reverse for '*' with arguments '()' and keyword arguments '{}' not found, Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found, Django change_password NoReverseMatch at /accounts/password/change/ (and a few more, I tried all the solutions there, but I can't find a problem in my own code)
Any help is appreciated. Thank you!
Ok, so the suggested solution for me didn't work here. I'm using Django 1.8.8 in an application with a specific app label, so I need to specify a url in a template like this e.g. app_label:url_name. This meant the reverse for password_change_done would never work since it's app_label:password_change_done.
But thankfully there's a solution: 'post_change_redirect'. Hence I specified password_change like this:
url(r'^password_change$', 'django.contrib.auth.views.password_change', {'template_name': 'password_change.html', 'post_change_redirect': 'app_label:password_change_done'}, name='password_change'),
I'm sure others could use this to overcome the problem above and still keep their own custom url name.
In some section you call the url named "password_change_done"
the correct name is: "userauth_password_change_done"
The solution was, that the in the urls.py the name of the password_change_done link must be 'password_change_done':
url(r'^password-change-done/$', 'password_change_done',
{'template_name': 'userauth/password_change_done.html'},
name='password_change_done'),
I had a look into django.contrib.auth.views.password_change (which was creating the problem) and realized, that the the url 'password_change_done' is hardcoded there in Django 1.8.2.
You need delete single quotes around the view name
{% url password_change_done %}
instead of
{% url 'password_change_done' %}
I meet the same kind of problem. The reason is the "password_change_done" is hard code in the auth.views, so I extense the class base on the auth.views.PasswordChangeView。
the auth.views.PasswordChangeView:
class PasswordChangeView(PasswordContextMixin, FormView):
......
success_url = reverse_lazy('password_change_done')
the MyPasswordChangeView:
class MyPasswordChangeView(PasswordChangeView):
success_url = reverse_lazy('app_label:password_change_done')
just overide the success_url.
and use MyPasswordChangeView in the urlpattern. Now, everything is ok.
in case if you are using
app_name
and trying to overwrite default template for update/change password you have to tell that Django:
from django.urls import path, reverse_lazy
from django.contrib.auth import views as auth_view
from . import views
app_name = 'account'
urlpatterns = [
path('login/', auth_view.LoginView.as_view(), name='login'),
path('logout/', auth_view.LogoutView.as_view(), name='logout'),
path('password_change/',
auth_view.PasswordChangeView.as_view(
template_name='registration/password_change_form.html',
success_url=reverse_lazy('account:password_change_done')), name='password_change'),
path('password_change/done/',
auth_view.PasswordChangeDoneView.as_view(
template_name='registration/password_change_done.html'), name='password_change_done'),
]

Django - NoReverseMatch

So I am trying to get into Django by following the base tutorial but changing it a but to make into something I would actually use.
I have these urls.py:
from django.conf.urls import patterns, url
from budget import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^category/$', views.category, name='category'),
url(r'^category/(?P<category_id>\d+)/$', views.category_detail, name='category_details')
)
and
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^budget/', include('budget.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And then I have the following template:
{% if all_categories_list %}
<ul>
{% for category in all_categories_list %}
<li>{{ category.category_text }}</li>
{% endfor %}
</ul>
And this from views.py
def category(request):
all_categories_list = Budget_category.objects.order_by('category_type')
context = {'all_categories_list': all_categories_list}
return render(request, 'budget/category_list.html', context)
Now what I want to do is to remove the hardcoded /budget/category/ from the template and replace it with the url keyword to make it more flexible.
So change the line in the template to this
<li>{{ category.category_text }}</li>
But that gives an error of the type on localhost/budget/category/
NoReverseMatch at /budget/category/
Reverse for 'category' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['budget/category/$']
Now I know this has something to do with either namespaces or the regular expressions ( I think so at least) but I seem to have not enough insight yet to see the issue. Cause this link localhost/budget/category/1/ does work the way it should. So why won't it build that link properly?
The name of your url is category_details, not the category. So change the {% url %} tag to:
{% url 'category_details' category.id %}

Django reverse error: NoReverseMatch

I've looked at a lot of different posts, but they're all either working with a different version of django or don't seem to work. Here is what I'm trying to do:
urls.py (for the entire project):
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
)
urls.py (specific to the app):
urlpatterns = patterns ('' ,
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>[\w\-]+)/$', views.posts, name="postdetail"),
)
views.py:
def index(request):
posts = Post.objects.filter(published=True)
return render(request,'blog/index.html',{'posts':posts})
def posts(request, slug):
post = get_object_or_404(Post,slug=slug)
return render(request, 'blog/post.html',{'post':post})
And finally the template:
{% block title %} Blog Archive {% endblock %}
{% block content %}
<h1> My Blog Archive </h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url "postdetail" slug=post.slug %}">
{{post.title}}
</a>
</h2>
<p>{{post.description}}</p>
<p>
Posted on
<time datetime="{{post.created|date:"c"}}">
{{post.created|date}}
</time>
</p>
</div>
{% endfor %}
{% endblock %}
For some reason this gives me a "No reverse Match": Reverse for 'postdetail' with arguments '()' and keyword arguments '{u'slug': u'third'}' not found. 0 pattern(s) tried: []
I've already tried getting rid of the double quotes around postdetail in the template, and I've also tried referring to it by the view name instead of the pattern name. Still no luck. The documentation isn't too helpful either.
Help is really appreciated! Thanks
You've used a namespace when including the URLs, so you probably need to use "blog:postdetail" to reverse it.

Categories

Resources