I am simply trying to open the web app to the home page, but at startup I am given the NoReverseMatch error message. The full message is the following: Reverse for 'wiki' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<title>[^/]+)/$']. In case it's relevant, I am using Django version 3.2.4.
In urls.py for my project I have:
urlpatterns = [
path('', include("encyclopedia.urls", namespace="encyclopedia")),
path('admin/', admin.site.urls),
]
In urls.py for my app I have:
app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<title>/", views.entry, name="wiki"),
.... (rest omitted for brevity)
]
In views.py I have:
def index(request):
context = {
"entries": ["item1", "item2", "item3"],
"form": SearchForm(),
}
return render(request, "encyclopedia/index.html", context)
In index.html, I have:
{% block body %}
<h1>All Pages</h1>
<ul>
{% for entry in entries %}
<li>
<form action="{% url 'encyclopedia:wiki' %}", method="POST">
{% csrf_token %}
<input type="text" value="{{ entry }}" name="link" hidden>
<input type="submit" class="link-btn" value="{{ entry }}" style="border: 0; background-color: white">
</form>
</li>
{% endfor %}
</ul>
{% endblock %}
As the error message indicates, it appears to be looking at the url pattern named wiki, and then subsequently failing to find the arguments passed to it. However, I am not trying to check that pattern; it should be going to the default pattern of path("", views.index, name="index"), not the wiki one. The wiki one works fine, by the way, as when I go the url wiki/<title>/, it loads fine. The rest of the app works fine as well. The other odd thing is that the index path worked fine before I added the arguments to the wiki url (path("wiki/<title>/", views.entry, name="wiki"),).
I have been looking at other SO posts that deal with this issue, and have followed the debugging advice given in this post, as well as this article, and none of the solutions apply/seem to work; this happens on app startup so there is no link being clicked to begin with, there are no comments it could be reading instead, it's not supposed to be calling wiki in the first place so the key word arguments are irrelevant, and the namespace is set in the project's urls.py. In summary, the issue appears to be that, upon start up of the app, it should be looking at the name "index" in the first path(), and then calling the appropriate function, but it is instead looking at the name "wiki" and expecting arguments.
If anyone can figure out how I can fix this problem, I would be very grateful!
Related
I am facing 2 issues NoReverseMatch and APPEND_SLASH .
Issue #1. APPEND_SLASH
Detail.html
<form action="update-entry" method="post">
/* if I add '/' at the end of update-entry, it works fine. */
{% csrf_token %}
{{ form }}
<input type="submit" value="Edit">
</form>
When I click on the Edit button, I get the error below,
You called this URL via POST, but the URL doesn't end in a slash and you have
APPEND_SLASH set. Django can't redirect to the slash URL while maintaining
POST data. Change your form to point to 127.0.0.1:8000/genericviews/1/update-
entry/ (note the trailing slash), or set APPEND_SLASH=False in your Django
settings.
This is the URL generated:
http://127.0.0.1:8000/genericviews/1/update-entry
I know URL should end with '/'.
urls.py
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailsView.as_view(), name='detail'),
url(r'^makeentry$', views.makeentry, name='makeentry'),
url(r'^static/$', views.StaticView.as_view()),
url(r'^new-entry/$', views.MakeEntryView.as_view(), name='new-entry'),
url(r'^(?P<pk>[0-9]+)/update-entry/$', views.UpdateEntryView.as_view(), name='update-entry'),
]
My confusion is why URL is not generating '/' at the end. Above URL pattern seems correct to me.
Issue #2 NoReverseMatch
When I try to change the hardcoded URL, I get the error below,
NoReverseMatch at /genericviews/1/
Reverse for 'update-entry' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['genericviews/(?P<pk>[0-9]+)/update-
entry/$']
Detail.html
<form action="{% url 'genericviews:update-entry' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Delete Product">
</form>
tried link as well,
{#Edit#}
When I click on any item from the page http://127.0.0.1:8000/genericviews/,
it takes me to the URL http://127.0.0.1:8000/genericviews/1/
And this is where it shows error.
I checked other answers, however, couldn't get it to work.
Any help would be appreciated.
It's not adding a slash because you haven't asked it to. You've hard-coded the relative URL of "update-entry", so that's what it will use.
When you do try and use the url tag, you get the error because you haven't passed the arguments it needs to generate that URL. Assuming you have the object in your template context as object, you would do:
{% url 'genericviews:update-entry' pk=object.pk %}
I'm receiving this error:
NoReverseMatch at /comments_page/1/post_comment/
Reverse for 'post_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['comments_page/(?P[0-9]+)/post_comment/$']
My views.py
def post_comment(request, product_id):
host_product = Product.objects.get(pk=product_id)
comment = Comment()
comment.product = host_product
comment.author = request.POST["author"]
comment.comment_text = request.POST["comment"]
comment.save()
return render(request, 'comments_page/detail.html', {"host_product": host_product})
My comments_page\urls.py
from django.conf.urls import url
from . import views
app_name = "comments_page"
urlpatterns = [
# /comments_page/
url(r'^$', views.index, name="index"),
# /comments_page/1/
url(r'^(?P<product_id>[0-9]+)/$', views.detail, name="detail"),
# /comments_page/1/post_comment/
url(r'^(?P<product_id>[0-9]+)/post_comment/$', views.post_comment, name='post_comment'),]
My detail.html
<form action="{% url 'comments_page:post_comment' product.id %}" method="post">
{% csrf_token %}
Name: <input type="text" id="author" name="author">
Comment:
<textarea id="comment" name="comment"></textarea><br>
<input type="submit" value="Post Comment">
I think I've identified the problem as being in the product.id here
{% url 'comments_page:post_comment' product.id %}
in the html page. I've tried formatting this a couple of different ways, but I haven't had any luck. Do note, the comment is going through and the form and it works as far as updating the database and loading the entry on the page goes, but the page is not being redirected. I have to reload it manually. Any help would be appreciated.
The error message shows that the argument you pass to the {% url %} tag does not exist and resolves to an empty string. Your view indeed does not pass in a product variable, only a host_product variable. You need to change the tag accordingly:
{% url 'comments_page:post_comment' host_product.id %}
For those who may wonder, the fix is to change the return function in views.py to this
return render(request, 'comments_page/detail.html', {"product": host_product})
I do not understand why this works, but it does. Any suggestions as to how to clean up my post_comment function would be appreciated. I feel it's overly convoluted by using host_product
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
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'),
]
I am running into a few issues regarding URL mappings in Django. I have the following code:
table.html:
<form id="filter_form" method="post" action="update_filters/">
<input type="submit" name="submit" value="Report" />
</form>
urls.py:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^update_filters/', views.filter_report, name='update_filters'),
]
views.py:
def filter_report(request):
# Code in the function
return render(request, 'autotester/table.html', context)
and everything works, but when I hit the "Report" button multiple times I get:
127.0.0.1:8000/autotester/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters
127.0.0.1:8000/autotester/update_filters/update_filters/update_filters
etc
and I have no idea what's causing it. There has to be some sort of simple fix for this but I just can't find it and I have been trying to figure this out for 3 hours now and my brain is just fried.
Try using {% url 'update_filters' %} template tag. And also add $ at the end of the regular expression in your url definition.
url(r'^update_filters/$', views.filter_report, name='update_filters'),