/signup处的运行时错误:更改表单指向127.0.0.1:8000/signup/,或者在Django设置中设置APPEND_SLASH=False

6jjcrrmo  于 2023-02-10  发布在  Go
关注(0)|答案(1)|浏览(312)

RuntimeError at /signup您通过POST调用了这个URL,但是该URL没有以斜杠结尾,并且您设置了APPEND_SLASH。Django在维护POST数据的同时无法重定向到斜杠URL。更改表单以指向127.0.0.1:8000/signup/(注意尾部斜杠),或者在Django设置中设置APPEND_SLASH=False。
我在使用表单时遇到了这个问题。请帮助我使用bootstrap进行样式设置

<form action="/signup/" method = "post" >
            {% csrf_token %}
            <div class="input-group mb-3">
                <span class="input-group-text" id="basic-addon1">@</span>
                <input type="text" class="form-control" placeholder="Username" name = "username" aria-label="Username" aria-describedby="basic-addon1">
            </div>

            <div class="input-group mb-3">
                <input type="text" class="form-control" placeholder="Firstname" name="fname" aria-describedby="basic-addon1">
            </div>

            <div class="input-group mb-3">
                <input type="text" class="form-control" placeholder="Lastname" name="lname" aria-describedby="basic-addon1">
            </div>

            <div class="input-group mb-3">
                <input type="email" class="form-control" placeholder="Recipient's email" name="email" aria-describedby="basic-addon2">
                <span class="input-group-text" id="basic-addon2">@gmail.com</span>
            </div>

            <div class="input-group mb-3">
                <input type="password" placeholder="password" name = "password" class="form-control">
            </div>

            <div class="input-group mb-3">
                <input type="password" placeholder="confirm password" name = "con_password" class="form-control">
            </div>
            
            
            <div class="input-group mb-3">
                <button type="submit" class="btn btn-primary">Signup</buttpn>
            </div>
        </form>
    </div>

Views.py

def signup(request):
    
    if (request.method == "POST"):
        username = request.POST.get("username")
        fname = request.POST.get("fname")
        lname = request.POST.get("lname")
        email = request.POST.get("email")
        password = request.POST.get("password")
        con_password = request.POST.get("con_password")
        
        User.objects.create_user(username=username, first_name=fname, last_name=lname, email=email, password=password)
        User.save()
        messages.success(request, "Account created successfully")
        return redirect("login")

请重定向到登录页面

4xy9mtcn

4xy9mtcn1#

在settings.py中添加APPEND_SLASH=False,或者将form action="/signup/"更改为form action="/signup"

相关问题