NoReverseMatch at account/reset/done in django password reset feature - python

I am using django(1.10) default password reset feature.I am getting the below error when I change the password from the password reset form.
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Template error:
In template C:\pyprojects\cias\ciasproj\ciassite\templates\registration\password_reset_complete.html, error at line 5
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 1 : {% extends 'base.html' %}
2 :
3 : {% block content %}
4 : <p>
5 : Your password has been set. You may go ahead and sign in now.
6 : </p>
7 : {% endblock %}
register/password_reset_complete.html
{% extends 'base.html' %}
{% block content %}
<p>
Your password has been set. You may go ahead and sign in now.
</p>
{% endblock %}
accounts/urls.py --
urlpatterns = [
url(r"^signup/$", views.signup, name="account_signup"),
#url(r'^login/$', views.login, {'template_name': 'accounts/login.html'}, name='login'),
url(r'^login/', views.login_view, name='account_login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/account/login'}, name='logout'),
url(r'^confirmemail/$', views.confirmemail, name='account_confirmemail'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',views.activate, name='activate'),
url(r'^activate/',views.activate, name='empty_activate'),
url(r'^password_reset/$', auth_views.password_reset, name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,23})/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
url(r'^reset/done/$', auth_views.password_reset_complete, name='password_reset_complete'),
]
I tried to modify the html link as
{% url reverse('account_login:login') %}
But that is giving another error --
Could not parse the remainder: '('account_login:login')' from 'reverse('account_login:login')'
Any help is highly appreciated. Thanks in advance.

I think it's a simple thing you are missing here. Whatever name you are giving there in the url list, you can use only that. If there is an app with a registered namespace, you have to use that.
In this case, you have defined the things directly and you should use the urls in that way only.
Instead of {% url 'login' %} it should be {% url 'account_login' %}.
Just the name you have given to url pattern.

Related

How do I fix a reverse error in Django when redirecting?

I am currently working on a website where you can create a shopping list. I am trying to insert items into the shoplist. So things like banana, cake, etc would go into shoplist. I got everything to work. When I create the item, it goes inside the database but when I try to redirect back to the website where I pressed create item, it shows the error
Reverse for 'detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['shoplist/(?P<item_id>[0-9]+)/$']
Also, when I try to enter my details page, it shows the error
Reverse for 'createitem' with arguments '('',)' not found. 1 pattern(s) tried: ['shoplist/(?P<item_id>[0-9]+)/createitem/$']
I think I did something wrong when making my paths or doing something wrong syntax wise. Nothing I found on the internet is fixing it. Is there a way to fix this problem? Thank you very much!
views.py
def createitem(request, item_id):
if request.method == 'GET':
return render(request, 'shoplist/createitem.html', {'form':ItemForm(), 'id':item_id})
else:
form = ItemForm(request.POST)
itemlist = form.save(commit=False)
itemlist.shoplist = Shoplist.objects.filter(user=request.user, pk=item_id).first()
itemlist.user = request.user
itemlist.save()
return redirect('detail', pk=item_id)
urls.py
from django.contrib import admin
from django.urls import path
from shoplist import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
#authentication
path('signup/', views.usersignup, name='usersignup'),
path('logout/', views.userlogout, name='userlogout'),
path('login/', views.userlogin, name='userlogin'),
path('create/', views.createlist, name='createlist'),
path('shoplist/', views.currentshoplist, name='currentshoplist'),
path('shoplist/<int:item_id>/', views.detail, name='detail'),
path('shoplist/<int:item_id>/createitem/', views.createitem, name='createitem'),
]
detail.html
{% extends 'shoplist/base.html' %}
{% block content %}
<h2>{{ error }}</h2>
<h1>{{ shopitems }}</h1>
{% for i in item %}
{{i.item}}
{% endfor %}
<form action="{% url 'createitem' item_id %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Create Item</button>
</form>
{% endblock %}
You want to redirect to the 'detail' view and the arg required for it is item_id as an int. I think you want redirect('detail', item_id=item_id). However, you probably want to get the created pk from the form... Maybe form.instance.pk? So redirect('detail', item_id=form.instance.pk). It's not clear if that form is for saving the same object type as you're viewing with the 'detail' view.
For the {% url %}, I don't think you can use anything but keyword args. So, {% url 'createitem' item_id=item_id %} if you put item_id into the template context.

NoReverseMatch - Resetting password in Django

I have found many similar questions to this issue. This question was one of them, but it didn't solve my problem, so I will ask my own question.
I'm making a password reset page on my website. But when I go to http://localhost:8000/users/reset-password and enter my email and clicks on 'Reset my password', then Django throws a NoReverseMatch error at me.
The error is:
NoReverseMatch at /users/reset-password/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
I believe there's something wrong with how I write my urlpatterns.
I've tried:
Making my own views and templates.
Rewriting all my urlpatterns.
My Code
urls.py:
"""Defines URL Patterns for users."""
from django.urls import re_path
from django.contrib.auth.views import (
LoginView, PasswordResetView, PasswordResetConfirmView,
PasswordResetDoneView,
)
from . import views
urlpatterns = [
# Login Page.
re_path(r'^login/$', LoginView.as_view(template_name='users/login.html'),
name='login'),
# Logout Page.
re_path(r'^logout/$', views.logout_view, name='logout'),
# Registration Page.
re_path(r'^register/$', views.register, name='register'),
# Password reset Page.
re_path(r'^password_reset/$', PasswordResetView.as_view(
# This is the only line I added in this file.
template_name='users/password_reset_email.html'
),
name='password_reset'),
# Password reset done Page.
re_path(r'^password_reset/done/$', PasswordResetDoneView.as_view(),
name='password_reset_done'),
# Password reset confirm Page.
re_path(r'^password_reset/confirm/'
+ '(?P<uidb64>[0-9A-Za-z]+)/(?P<token>.+)/$',
PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
]
My own users/password_reset_email.html:
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'users:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
Update:
I got something right. Now I get a NoReverseMatch at /users/password_reset/
Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': '', 'token': ''}' not found. 1 pattern(s) tried: ['users/password_reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$']. I got to this error by using my own template of djangos password_reset_email.html, where I modified the line: {% url 'password_reset_confirm' uidb64=uid token=token %} to {% url 'users:password_reset_confirm' uidb64=uid token=token %}. Now i'm almost certain that i'm just writing my urls or regexes wrong.
I have edited my question to show the new code.
I finally got it.
Here is the answer to the problem.
Now I get a ConnectionRefusedError, which means that I now only need to set up an SMTP Server for emails, and then it should work! The thing I was missing all the time was, that I wasn't pointing out what email template I wanted to use. I just set the email template as the template, so Django couldn't render it correctly. Here is the updated code for urls.py where path(r'password-reset/') is changed:
from django.urls import path, reverse_lazy
import django.contrib.auth.views as auth_views
from . import views
urlpatterns = [
# Login Page.
path(r'login/', auth_views.LoginView.as_view(
template_name='users/login.html'
),
name='login'),
# Logout Page.
path(r'logout/', views.logout_view, name='logout'),
# Registration Page.
path(r'register/', views.register, name='register'),
# Password reset page.
path(r'password-reset/', auth_views.PasswordResetView.as_view(
email_template_name='users/password_reset_email.html',
success_url=reverse_lazy('users:password_reset_done')
), name='password_reset'),
# Password reset done page.
path(r'password-reset/done/',
auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
# Password reset confirm page.
path(r'password-reset/confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm')
]
Everything else is correct.
I got the answer from this answer.

Django Error passing URL argument from Template to View: NoReverseMatch Reverse not found. 1 pattern(s) tried

I am trying to pass configure a URL like so:
/details/12345
Template HTML:
<div class="row">
{% if article_list %}
{% for article in article_list %}
<div>
<h2>{{ article.title }}</h2>
<p>{{ article.body }}</p>
<p><a class="btn btn-default" href="{% url 'details' article.id %}" role="button">View details ยป</a></p>
</div><!--/.col-xs-6.col-lg-4-->
{% endfor %}
{% endif %}
</div><!--/row-->
urls.py (full):
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'news_readr.views.home', name='home'),
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py:
from django.shortcuts import render
from .models import Article
# Create your views here.
def home(request):
title = "Home"
article_list = Article.objects.all()
for article in article_list:
print(article.id)
context = {
"title": title,
"article_list": article_list,
}
return render(request, "home.html", context)
def details(request, article_id = "1"):
article = Article.objects.get(id=article_id)
return render(request, "details.html", {'article': article})
I am getting an error that says:
NoReverseMatch at /
Reverse for 'details' with arguments '()' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/(?P<article_id>\\d+)/$']
I'm one week old at Django, and I think there's something wrong with my URL Named Group config. Please help! TIA!
Update: If I remove the URL config and change it back to:
url(r'^details/$', 'news_readr.views.details', name='details'),
The error changes to:
Reverse for 'details' with arguments '(1,)' and keyword arguments '{}'
not found. 1 pattern(s) tried: ['details/$']
So it seems to be picking up the argument that is passed 1 in this case. So this seems to be an issue with the regular expression. I tried the expression out at Pythex, but even there, the expression doesn't seem to be matching anything.
For the url pattern
url(r'^details/(?P<article_id>\d+)/$', 'news_readr.views.details', name='details'),
The correct way to use the tag is
{% url 'details' article.id %}
This because the details url pattern has a group article_id, so you have to pass this to the tag.
If you have the above url pattern, and {{ article.id}} displays correctly in the template, then the above template tag should not give the error Reverse for 'details' with arguments '()'. That suggests you have not updated the code, or you have not restarted the server after changing code.
If you change the url pattern to
url(r'^details/$', 'news_readr.views.details', name='details')
then you need to remove the article.id from the url tag.
{% url 'details' %}
I guess your pattern is wrong.( not an expert of regex ).
Try this
url(r'^details/((?P<article_id>[0-9]+)/$', 'news_readr.views.details', name='details'),

NoReverseMatch in Django (url ploblem)

I'm a beginner of Django
I want to set my url with database field_name instead of use primary key from Django tutorial. This is my code.
*mysite*
**dwru/urls.py**
urlpatterns = [
url(r'^$', include('product.urls', namespace="product")),
]
*myapp*
**product/urls.py**
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<name_catalog>[-_\w]+)/$', views.product_list_from_index, name='catalog'),
]
**product/views.py**
def product_list_from_index(request, name_catalog):
catalog = get_object_or_404(Catalog, name_catalog=name_catalog)
context = {
'catalog': catalog
}
return render(request,'product/product_list.html', context)
**product/models.py**
class Catalog(models.Model):
name_catalog = models.CharField(max_length=45)
product = models.ManyToManyField(Product)
**template/product/index.html**
{% for catalog in catalog_list %}
<h1><a href="{% url 'product:catalog' catalog.name_catalog %}">{{ catalog.name_catalog }}</h1>
{% endfor %}
Then I add Catalog field with "TestCa01" then it shown an error
NoReverseMatch at /
Reverse for 'catalog' with arguments '(u'TestCa01',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$(?P<name_catalog>[-_\\w]+)/$']
it kind of some problem with this line in template
{% url 'product:catalog' catalog.name_catalog %}
any Idea?
The $ in your first url pattern might be the issue. The dollar sign indicates that it's the the end of the pattern, so it would never bother to include/check the product urls. Is the index url working?

Passing an id to a url link - Django

I need to pass a cluster_id in my show.html page, in case the user wishes to halt a cluster created. My urls.py looks like:
from django.conf.urls import url
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from clusters import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/', views.register, name='register'),
url(r'^show/', views.show, name='show'),
url(r'^login/', views.user_login, name='login'),
url(r'^logout/', views.user_logout, name='logout'),
url(r'^destroy_clusters/', views.destroy_clusters, name='destroy_clusters'),
url(r'^halt_clusters/(?P<cluster_id>\d+)/$', views.halt_clusters, name='halt_clusters'),
url(r'^check_log_status/', views.check_log_status, name='check_log_status'),
url(r'^read_cluster_log/', views.read_cluster_log, name='read_cluster_log'),
]
and in my show.html I have:
{% for cluster in clusters %}
<tr>
<td>{{ cluster.id }}</td>
<td>Halt</td>
<tr>
{% endfor %}
so when I execute the webpage, I have:
NoReverseMatch at /clusters/register/
Reverse for '' with arguments '()' and keyword arguments '{u'cluster_id': 19L}' not found. 0 pattern(s) tried: []
and this is driving me crazy!! Why can't I just pass the id there? I looked this example: Passing an id in Django url but it doesn't seem to work for me. Help?
My Django version is 1.8.dev
In recent Django's you should quote literal URL names in the tag:
{% url "halt_clusters" cluster_id=cluster.id %}
Otherwise Django will look for halt_clusters variable in your context and use its value as the URL name.
Oh, and while you're at it, you don't have to specify the keyword parameter name, so this should work as well:
{% url "halt_clusters" cluster.id %}

Categories