Django表示缺少方法=“post”action =“”

时间:2017-05-16 06:19:37

标签: django

美好的一天,

使用Django 1.11,我创建了登录和注册表单。

我的登录表单工作正常,但我的注册表单使用的是GET方法,而不是指定的POST方法。在登录表单上使用检查器,它只显示 method =“POST”action =“....”缺失,我看不出原因。

urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'signin/$', views.sign_in, name='signin'),
    url(r'signup/$', views.sign_up, name='signup'),
    url(r'signout/$', views.sign_out, name='signout'),
]

views.py

def sign_in(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            if form.user_cache is not None:
                user = form.user_cache
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect(
                        reverse('stb:home')
                    )
                else:
                    messages.error(
                        request,
                        "That user account has been disabled."
                    )
            else:
                messages.error(
                    request,
                    "Username or password is incorrect."
                )
    return render(request, 'stb/signin.html', {'form': form})

def sign_up(request):
    form = UserCreationForm()
    if request.method == 'POST':
        form = UserCreationForm(data=request.POST)
        if form.is_valid():
            # Unpack form values
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            email = form.cleaned_data['email']
            # Create the User record
            user = User(username=username, email=email)
            user.set_password(password)
            user.save()

            user = authenticate(
                username=username,
                password=password
            )
            login(request, user)
            messages.success(
                request,
                "You're now a user! You've been signed in, too."
            )
            return HttpResponseRedirect(reverse('stb:profile'))
    return render(request, 'stb/signup.html', {'form': form})

signup.html:

{% extends "layout.html" %}

{% block title %}{{ block.super }} | Sign Up{% endblock %}

{% block body %}
<div class="grid-30 centered">
    <h2>Sign Up</h2><form>
    <form method="POST" action="{% url 'stb:signup' %}">
        {% csrf_token %}
        <input name="username" id="id_username" required="" autofocus=""
           placeholder="User Name" maxlength="150" type="text">
        <input name="email" id="id_email" required=""
           placeholder="Email Address" type="email">
        <input name="password1" required="" id="id_password1"
           placeholder="Password" type="password">
        <input name="password2" required="" id="id_password2"
           placeholder="Confirm Password" type="password">
        <input type="submit" class="button-primary" value="Sign Up">
        <a class="button" href="signin.html">Sign In</a>
        <input type="hidden" name="next" value="{{ next }}" />
    </form>
</div>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

尝试将第一行中的表单分配移动到if,

的else语句

像这样,

def sign_up(request):
    if request.method == 'POST': 
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            email = form.cleaned_data['email']
            user = User(username=username, email=email)
            user.set_password(password)
            user.save()
            user = authenticate( username=username, password=password )
            login(request, user)
            messages.success( request, "You're now a user! You've been signed in, too." )
            return HttpResponseRedirect(reverse('stb:profile'))
        else:
            return render(request​, 'stb/signup.html', {'errors': form.errors}
    else:
        form = UserCreationForm ()
    return render(request, 'stb/signup.html', {'form': form})