首页 > 解决方案 > 我在 python 中的 POST 方法有问题

问题描述

我正在使用 windows(10) 和 django 库我想在我的电子商务中创建注册选项,所以我制作了一个名为 accounts 的应用程序,我在它的视图中写道

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm

def signup(request):
if request.method == 'POST':
    form = UserCreationForm(request.POST)
    print('In Post')
    if form.is_valid:
        form.save()
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        print(username , password)

else:
    form = UserCreationForm()

    context = {'form' : form}
    return render(request , 'registration/signup.html' , context)

我在我的 signup.html 页面中写道

{% extends "base.html" %}

{% block body %}
<main class="mt-5">
    <div class="container mynav">
        <form action="" method="post">
            {% csrf_token %}
            {{form}}
            <button type="submit" class="btn btn-primary">Sign up</button>
        </form>
    </div>
</main>
{% endblock body %}

当我尝试运行 POST 方法时出现问题,他说
The view accounts.views.signup didn't return an HttpResponse object. It returned None instead.
在 CMD(=terminal)The User could not be created because the data didn't validate.
中我写什么来解决这个问题?谢谢。

标签: pythondjangopost

解决方案


推荐阅读