Django sign up page shows alert "Something went wrong, try again" instead of success message

I am working on Django project.

I have a user_signup page where users can create an account.

When I click Create an Account, and when i create it with users informations , instead of showing "signed up successfully" I get a browser alert "Something went wrong, try again"

The page URL is

http://127.0.0.1:8000/user_signup/

I want to show success message after successful signup

views.py

def user_signup(request):
    error=""
    if request.method=='POST':
        f=request.POST['fname']
        l=request.POST['lname']
        i=request.FILES['images']
        p=request.POST['pwd']
        e=request.POST['email']
        con=request.POST['contact']
        gen=request.POST['gender']
        try:
            user=User.objects.create_user(first_name=f, last_name=l, username=e, password=p)
            StudentUser.objects.create(user=user, mobile=con, image=i, gender=gen, type=student)
            error="no"
        except:
            errror="yes"
            d={'error':error}
    return render(request, 'user_signup.html', d)

html(user_signup)


{% if user error == "no" %}
    <script>
        alert("Signup Successfully");
        window.location("{% url 'user_login' %}")
    </script>
{% endif %}
{% if error =="yes"}
    <script>
        alert("Something went wrong, try again");
    </script>
{% endif %}

what I am doing wrong, and how can i properly show a success message after signup ?

As I see, you had multiple small issues, not just one.

In views.py:

  • Typo: errror="yes" → should be error="yes"

  • d was only defined in except but used in return

  • return indentation was wrong

  • type=student → should be a string: type="student"

In the template:

  • {% if user error == "no" %}{% if error == "no" %}

  • window.location(...)window.location.href = ...

  • {% if error =="yes"} → missing %

So the views.py shall be:

def user_signup(request):
    error = ""
    d = {'error': error}

    if request.method == 'POST':
        try:
            user = User.objects.create_user(
                first_name=request.POST['fname'],
                last_name=request.POST['lname'],
                username=request.POST['email'],
                password=request.POST['pwd']
            )
            StudentUser.objects.create(
                user=user,
                mobile=request.POST['contact'],
                image=request.FILES['images'],
                gender=request.POST['gender'],
                type="student"
            )
            error = "no"
        except Exception:
            error = "yes"

        d = {'error': error}

    return render(request, 'user_signup.html', d)

and for template:

{% if error == "no" %}
<script>
    alert("Inscription réussie !");
    window.location.href = "{% url 'user_login' %}";
</script>
{% endif %}

{% if error == "yes" %}
<script>alert("Erreur, réessayez");</script>
{% endif %}

That's all as I think

Вернуться на верх