NoReverseMatch at /login.html/

Question:

this is the html form. I added action="{% url ‘create_user_view’ %}" to the html but the error says it is not a valid view.

{% extends 'base.html' %}
2   
3   {% block content %}
4   
5   
6       <div class="formbox">
7           <form method="post" action="{% url 'create_user_view' %}">
8               <h1 class="max"> Sign up </h1>
9               <input type ="text" class="max" id="fname" placeholder="username" name="username">
10              <br>
11              <input type="email" class="max" id="email" placeholder="email" name="email">
12              <br>
13              <input type="password" class="max" id="password" placeholder="password" name="password">
14              <br>
15              <input type="submit" name="submit" class="max" placeholder="SUBMIT" id="submitid" formaction="logbook.html">
16              <br>
17              <a href="home.html" id="links">already have an account?</a>

Given below is the views.py file. I have created a view called create_user_view in the views.py file.
However, the html form action does not find a valid view called create_user_view.

from django.shortcuts import render

from django.contrib.auth.models import User
from django.shortcuts import render, redirect


# Create your views here.

def home_screen_view(request):
    print(request.headers)
    return render(request, "personal/home.html",{})

def login_view(request):
    print(request.headers)
    return render(request,"personal/login.html",{})


def create_user_view(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')
    
        
        user = User.objects.create_user(username=username, password=password, email=email)
        # You can add additional fields to the User model as needed
        
        # Redirect to a success page
        return redirect('success')
        
    # If the request method is not POST, render the form
    return render(request, "create_user.html")

This is the urls.py file

from django.contrib import admin
from django.urls import path

from personal.views import(
    home_screen_view, login_view,create_user_view,
    )

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", home_screen_view),
    path("login.html/", login_view),
    path("create_user_view/", create_user_view),

]

Asked By: Muhammed

||

Answers:

The urls tag {% url ‘create_user_view’ %} references the name of the path in urlspattern, so you need to add a name:

urls.py

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", home_screen_view),
    path("login.html/", login_view),
    path("create_user_view/", create_user_view, name='create_user_view'),

]
Answered By: Razenstein
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.