Python Django NoReverseMatch Template - python

I configured the path and made the template but when I try to run server it gives
NoReverseMatch at /password-reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://localhost:8000/password-reset/
Django Version: 3.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
error.
My urls.py
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name= 'register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),
name='password_reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),
name='password_reset_done '),
path('password-reset-confirm///',
auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),
name='password_reset_confirm '),
path('', include('blog.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and my template
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class = "content section">
<form method="POST">
{% csrf_token %}
<fieldset class= "form-group">
<legend class = "border-bottom mb-4">Reset Password</legend>
{{form|crispy}}
</fieldset>
<div class = "form-group">
<button class="btn btn-outline-info" type ="submit"> Reset Password </button>
</div>
</form>
</div>
{% endblock content %}
Could you please tell me what is wrong with my project? Thank you

It looks like you got a space in the name of 'password_reset_confirm '
'password_reset_confirm '
^
Not sure how django well django handles that. If that is not the issue, please post the code where you reverse-call the URL in the template (where the {% url ... %} template tag is or possibly from your view where you configured your e.g. success URL.

Related

Django: Reverse for 'login' not found. 'login' is not a valid view function or pattern name

I'm working on my project for a course and I'm totally stuck right now.
Login doesn't work. I using class-based views. When I`m logged in, everything works.The Logout View works too.
The error is as follows:
NoReverseMatch at /
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Exception Location: /home/aleo/#HOME/.virtualenvs/znajdki/lib/python3.8/site-packages/django/urls/resolvers.py, line 685, in _reverse_with_prefix
Python Executable: /home/aleo/#HOME/.virtualenvs/znajdki/bin/python3
Python Version: 3.8.6
My files:
poszukiwania/urls.py
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name = 'poszukiwania'
urlpatterns=[
path('<slug:kategoria_slug>/', views.rzeczy_list, name='rzeczy_list_by_category'),
path('<int:year>/<int:month>/<int:day>/<slug:rzecz_slug>/<int:id>', views.rzecz_detail, name='rzecz_detail'),
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('', views.rzeczy_list, name='rzeczy_list'),
]
znajdki/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('poszukiwania.urls', namespace='poszukiwania'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL)
settings.py
LOGIN_REDIRECT_URL = 'poszukiwania:rzeczy_list'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
templates/registration/login.html
{% extends "poszukiwania/base.html" %}
{% load static %}
{% block title %}Logowanie{% endblock %}
{% block content %}
<h1>Logowanie</h1>
{% if form.errors %}
<p>
Nazwa użytkownika lub hasło są nieprawidłowe.
Spróbuj ponownie
</p>
{% else %}
<p>Wypełnij aby się zalogować</p>
{% endif %}
<div class="login-form">
<form action="{% url 'poszukiwania:login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Zaloguj się"></p>
</form>
</div>
{% endblock %}
help me, please

AttributeError: module 'users.views' has no attribute 'register'

I am new to Python and Django. Getting this error when running the server. The problem seems to be in 'urls.py' of the project.
File "C:\Users\Desktop\myproject\django_project\django_project\urls.py", line 22, in <module>
path('register/', user_views.register , name = 'register'),
AttributeError: module 'users.views' has no attribute 'register'
This is my urls.py
from django.contrib import admin
from django.urls import path, include
from users import views as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register , name = 'register'),
path('', include('blog.urls')),
]
This is register.html under users/templates/users/register.html (users is the name of the app)
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Already Have An Account? <a class="ml-2" href="#">Sign In</a>
</small>
</div>
</div>
{% endblock content %}
This is my views.py
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
def register(request):
form = UserCreationForm()
return render(request, 'users/register.html', {'form':form})
I have also tried creating a separate urls.py for my 'users' app and adding the path there but I am getting the same error.
I am very new to programming so please excuse my inexperience.
Make sure you have a space after register>>> def register (request):
If it is not the issue please check the "views.py" for correct register function under the users folder.
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
# Create your views here.
def register (request):
form = UserCreationForm()
return render(request, 'users/register.html', {'form': form})
Your urls.py should be like this:
from django.contrib import admin
from django.urls import path, include
from .view.py import register
urlpatterns = [
path('admin/', admin.site.urls),
path('register/',register , name = 'register'),
path('', include('blog.urls')),
]
Note: if your views name is views.py
consider this arrange:
you have a users application in your base directory, this app has a users-views.py file that your register function is there.
then your urls.py must be like this:
from django.contrib import admin
from django.urls import path, include
from users import users-views as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register , name = 'register'),
path('', include('blog.urls')),
]
as you can see your users application has no views.py I recommend rename users-views.py to views.py and use your original urls.py.
Is your users/views.py file saved and up to date? Try stopping the server completely, ctrl+c on windows if it's started and double check all your files are up to date then try running the server again.

why my app_name show as plural by s by navigation into URL link? [login]

when I do submit this form why has been submitted by accounts/profile? I've no any file called accounts basically. my app_name calls account not accounts.
so, How can I submit my form according to app_name that I using Image Error
urls.py
from . import views
from django.conf.urls import url
from django.contrib.auth.views import LoginView, logout
app_name = 'account'
urlpatterns = [
# /account/
url(r'^$', views.index, name="home"),
# /account/login/
url(r'^login/$', LoginView.as_view(template_name='account/login.html'), name='login_page'),
# /account/logout/
url(r'^logout/$', logout, {'template_name': 'account/logout.html'}, name='logout'),
# /account/register/
url(r'^register/$', views.register, name='register'),
# /account/profile/
url(r'^profile/$', views.view_profile, name='view_profile'),
# /account/profile/edit/
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
# /account/profile/edit/
url(r'^change-password/$', views.change_password, name='change_password'),
]
login.html
{% extends 'base.html' %}
{% block title %} Login {% endblock %}
{% block body %}
<div class="container">
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</div>
{% endblock %}
I've no more files to explain that
I think these files just show you what happens when logged in
You must provide LOGIN_REDIRECT_URL.
If LoginView called via POST with user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified in next. If next isn’t provided, it redirects to settings.LOGIN_REDIRECT_URL (which defaults to /accounts/profile/). If login isn’t successful, it redisplays the login form. See Django documentation

Reverse for 'details' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['entry\\/(?P<pk>[0-9]+)$']

I have this error:
Reverse for 'details' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['entry\/(?P[0-9]+)$']
On line number 7:
From _calender_entry.html
<div class="calender_entry col-lg-4">
<h2> {{ entry.name }} </h2>
<h4> {{ entry.date }} </h4>
<p>
{{ entry.description }}
</p>
<button onclick="window.location='{% url 'details' pk=entry.id %}'" class="btn btn-primary"> View Details </button>
</div>
This is from my urls.py file:
urlpatterns = [
path('', views.index, name='index'),
path('entry/<int:pk>', views.details, name='details'),
path('admin/', admin.site.urls),
]
And here is method from views.py :
def index(request):
entries = Entry.objects.all()
return render(request, 'myapp/index.html', {'entries' : entries})
def details(request, pk):
entry = Entry.objects.get(id=pk)
return render(request, 'myapp/details.html', {'entry' : entry})
Here is models.py
class Entry(models.Model):
name = models.CharField(max_length=100)
date = models.DateTimeField()
description = models.TextField()
created = models.DateTimeField( auto_now_add = True )
def __str__(self):
return f'{self.name} {self.date}'
The _calender_entry.html is included in index.html file as follows:
{% block content %}
<div class="container">
<div class="row">
{% for entry in entries %}
{% include 'myapp/_calender_entry.html' %}
{% endfor %}
</div>
</div>
Could anyone be able to tell me how could I fix this problem?
now django forces you that each app has its own urls.py.
Create the urls.py file in the myapp/ directory
# myapp/urls.py
from django.urls import path
from myapp import views
urlpatterns = [
path('', views.index, name='index'),
path('entry/<int:pk>/', views.details, name='details'),
]
the urls.py of the project has to be as follows
# calenderproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(('myapp.urls', 'myapp'), namespace='myapp')),
]
use in the template as follows
# myapp/_calender_entry.html
View Details
that's how it works, I've checked
I found the error, delete this code from the template _calender_entry.html
<!-- {% include 'myapp/_calender_entry.html' %} -->
although this commented with html django equal it executes it to comment in the template uses {# {% include 'myapp / _calender_entry.html'%} #} in this way django does not execute it

why isn't this django url redirecting?

After getting post data from the following form, the page should redirect to 'associate:learn' as shown in the action. However, it just stays on the radio button page. I suspect I'm making a beginner's error but after rereading the tutorial, I'm not sure what's going on.
index.html
Choose a dataset
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
<input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
<label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^images/', include('images_app.urls', namespace="images_app")),
url(r'^associate/', include('associate.urls', namespace="associate")),
url(r'^admin/', include(admin.site.urls)),
)
views.py
def index(request):
images = Image.objects.all()
datasets = []
for i in images:
if i.rank() >= 3:
datasets.append(i)
return render(request, 'associate/index.html', {'datasets':datasets})
The original HTML should redirect to this page.
learn.html
THIS IS THE LEARN PAGE
Can you go to associate:learn directly?
In your first urls.py
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
The url will always match "associate.views.index" since it appears before "associate.views.learn" and they both have the same url.
You should change it to something like:
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)
Hope this helps.
Your associate "index" and "learn" views both have the same URL. You need to have some way of distinguishing between them, otherwise the URL will always be served by the first one, which is index.

Categories

Resources