Django error: NoReverseMatch

Question:
I am learning to create websites using the django framework in python. Now I want to implement account login and registration on my website. To create an account on the site, you need to enter your username and enter your password twice.

I want to make it so that when a user clicks on the “submit” button and if his two entered passwords are different, the message “Error: Passwords don’t match” appears. But I can’t implement it because of this error:

Part of the code from the file urls.py:

from django.urls import path
from . import views

app_name = "authentication"

urlpatterns = [
    path("signup/", views.signup, name="signup"),  # registration
    path("signup/n/", views.signup_n, name="signup_n"),  # registration processing
    path("signupe/", views.signup_error, name="signup_error")  # if an error occurred during registration
]

Part of the code from the file views.py:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth import login


def signup(request):
    return render(request, "authentication/signup.html")

def signup_error(request, text_error):
    return render(request, "authentication/signup.html", {"error": text_error})

def signup_n(request):
    username = request.POST["username"]
    password = request.POST["password"]
    password2 = request.POST["password2"]
    if password2 == password:
        user = User.objects.create_user(username=username, password=password)
        user.save()
        user_ = authenticate(request, username=username, password=password)
        login(request, user_)
        return HttpResponseRedirect(reverse("articles:index"))
    else:
        return HttpResponseRedirect(reverse("authentication:signup_error", args=("Passwords don't match.",)))

Part of the code from the file signup.html:

{% extends 'base.html' %}

{% block title %}Sign Up{% endblock %}

{% block content %}


{% if error %}
Error: {{error}}<br>
{% endif %}

<form action="{% url 'authentication:signup_n' %}" method="POST">
    {% csrf_token %}
    <input type="text" required placeholder="Your username" name="username" size="30" maxlength="50"><br>
    <input type="password" required placeholder="Your password" name="password" size="30"><br>
    <input type="password" required placeholder="Your password (again)" name="password2" size="30"><br>
    <button type="submit">Create article</button>
</form>

{% endblock %}

This project is not laid out in Replit. I asked for help here because this is the only programmers forum I am in.

If there is no solution to the problem here, I will ask for help in other places.

1 Like

Hey there @KAlexK !

From what I could see, you’re trying to pass an argument to a URL pattern that doesn’t accept one.

For example, in your urls.py , the signup_error path doesn’t have a spot for an argument, yet in your views.py, you’re attempting to pass an argument into it.

So you could modify your urls.py and views.py files to accept an argument.

For example, in your urls.py:

urlpatterns = [
    # ...
    path("signupe/<str:text_error>/", views.signup_error, name="signup_error")
]

And in your views.py:

def signup_n(request):
    # ...
    else:
        return HttpResponseRedirect(reverse("authentication:signup_error", args=("Passwords don't match.",)))

This will pass the text “passwords don’t match” as an argument to the signup_error view.

That being said…

I would mostly recommend that you use Django Message Framework, instead of doing all this:
You can see their documentation here:

And a good tutorial here:
https://ordinarycoders.com/blog/article/django-messages-framework

3 Likes

Thank you very much! I will review the Django documentation.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.