简体   繁体   中英

Django / HTML Submit button redirecting to the wrong page

I am trying to make a registration form with Django & HTML and I am following this tutorial: Video

2:45:00 into the video, I do the exact same steps as him, even though the only difference in my code is related to my previous question: My previous thread

This is my HTML code:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Registration</title>
</head>
<body>
    <form action="register" method="post">
        {% csrf_token %}
        <input type="text" name="first_name" placeholder="First Name"><br>
        <input type="text" name="last_name" placeholder="Last Name"><br>
        <input type="text" name="username" placeholder="Username"><br>
        <input type="email" name="email" placeholder="Email"><br>
        <input type="password" name="password1" placeholder="Password"><br>
        <input type="password" name="password2" placeholder="Confirm Password"><br>
        <input type="Submit">
    </form>
</body>
</html>

and this is my views.py:

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

def register(request):

    if (request.method == 'post'):
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        username = request.POST['username']
        password1 = request.POST['password1']
        password2 = request.POST['password2']
        email = request.POST['email']

        user = User.objects.create_user(username=username, password=password1, email=email, first_name=first_name, last_name=last_name)
        user.save()
        print('user created')
        return redirect('')

    else:
        return render(request, 'register.html')

However, it seems that when I press "Submit", instead of the button actually reading the views.py code and check the IF statement, it just redirects me to localhost:8000/account/register/register, which is completely wrong, such as shown here: Imgur link

I am unsure of what I am doing wrong, and why my code is acting differently even though I am doing exactly what the tutorial guy is doing in his video?

Any help would be much appreciated.

His code is simply wrong. You can't just put a view name in the action attribute like that; it needs to be an actual path. Since your URL is "/account/register", that's what you should use.

But a decent tutorial would have introduced the {% url %} tag. And, in fact, a decent tutorial would have used Django forms here; he seems to have completely neglected to think about validation, which is pretty inexcusable. I'd recommend finding a different tutorial.

(Note, you've made errors yourself in copying his code; in the view where you check request.method it must be "POST", not "post".)

The solution can be following:

if (request.method == '**POST**'):
    return redirect('**/**')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM