this is a shopping website my login and registration form is not working it is showing that no reverse path found error. this is my navbar template
<div >
<p >If you already have an account, <a href="{% url
'sweet:register_request' %}">login</a> instead.</p>
</div>
<div >
<p >Don't have an account? <a href="/register">Create an account</a>.
</p>
</div>
login.html this is my login template
{% extends "base.html" %}
{% load static %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ login_form }}
<button type="submit">Login</button>
</form>
{% endblock %}
home.html this is my registration template
{% extends "base.html" %}
{% load static %}
{% block content %}
<form method="POST">
<button type="submit">Register</button>
{% csrf_token %}
{{register_form}}
</form>
{% endblock %}
views.py register_request is function defined to register the form and login_request is to login into my website
def register_request(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registration successful.")
return redirect('login_request')
messages.error(request, "Unsuccessful registration. Invalid information.")
form = NewUserForm()
return render(request,"home.html",{"register_form": form})
def login_request(request):
if request.method == "POST":
form = AuthenticationForm(request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
messages.info(request, "You are now logged in as {username}.")
return redirect("base.html")
else:
messages.error(request, "Invalid username or password.")
else:
messages.error(request, "Invalid username or password.")
form = AuthenticationForm()
return render(request, 'login.html',{"login_form": form})
urls.py
from django.contrib.auth.views import LogoutView, LoginView
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name="sweet"
urlpatterns = [
path('',views.allProdCat,name='allProdCat'),
path('<slug:c_slug>/',views.allProdCat,name='products_by_category'),
path('<slug:c_slug>/<slug:prod_slug>/', views.ProductDetail, name='ProdCatDetail'),
path('register/', views.register_request, name="register"),
path("login/", views.login_request, name="login"),
]
forms.py the form is for registration and login
from django import forms
from django.contrib.auth.models import User
from . import models
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class NewUserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
this are all my code please help
CodePudding user response:
You are trying to get address of views.register_request
in {% url 'sweet:register_request' %}
but the name of url path you have chosen is different. You can do one of two options, in both you need to change name.
Option 1:
# from
path('register/', views.register_request, name="register"),
# to
path('register/', views.register_request, name="register_request"),
Option 2:
# from
{% url 'sweet:register_request' %}
# to
{% url 'sweet:register' %}
CodePudding user response:
instead of this:
<div >
<p >If you already have an account, <a href="{% url
'sweet:register_request' %}">login</a> instead.</p>
</div>
<div >
<p >Don't have an account? <a href="/register">Create an account</a>.
</p>
</div>
Do this:
<div >
<p >If you already have an account, <a href="{% url
'sweet:register' %}">login</a> instead.</p>
</div>
<div >
<p >Don't have an account? <a href="/register">Create an account</a>.
</p>
</div>