为什么我的django表单作为GET发送然后在POST时发出405错误

时间:2016-02-09 09:38:45

标签: python django django-forms django-views

我正在尝试在Django中创建一个“更改电子邮件”表单。表单显示在模板中,但是当我提交表单时,这会发生在终端

[09/Feb/2016 09:19:55] "GET /change_email/ HTTP/1.1" 200 3775
[09/Feb/2016 09:19:55] "GET /static/css/styles.css HTTP/1.1" 304 0
[09/Feb/2016 09:20:09] "POST /account/ HTTP/1.1" 405 0

然后发送到空白页面。我也查看了视图,并注意到,在一些打印语句中,它将转到最后的其他语句,而不是通过if request.POST:

这是我的代码:

查看:

def change_email(request):
    if request.POST:
        print "POST"
        form = ChangeEmailForm(request.POST)

        if form.is_valid():
            email = request.POST['current_email']
            email_new = request.POST['new_email']
            email_confirmed = request.POST['confirm_email']
            form.save()
            if email_new == email_confirmed:
                User.objects.get(email=email).update(email=request.POST['new_email'])
            else:
                return HttpResponse("These emails don't match")
        else:
            return render(request, 'registration/email_change_form.html', {'form': form})
    else:
        print "GET"
        form = ChangeEmailForm()
        return render(request, 'registration/email_change_form.html', {'form': form})

形式:

class ChangeEmailForm(forms.ModelForm):
    current_email = forms.EmailField()
    new_email = forms.EmailField()
    confirm_email = forms.EmailField()

    class Meta:
        model = User
        fields = ('current_email', 'new_email', 'confirm_email')

模板:

<form action="{% url 'account' %}" method="post">
    {% csrf_token %}
    <div class="form-group">
        <label for="Current Email">Current Email:</label>
        <div class="col-sm-10">
            {% render_field form.current_email type="email" class+="form-control" placeholder="Current Email" %}
        </div>
    </div>
    <div class="form-group">
         <label for="New Email">New Email:</label>
         <div class="col-sm-10">
             {% render_field form.new_email type="email" class+="form-control" placeholder="New Email" %}
         </div>
    </div>
    <div class="form-group">
         <label for="Confirm New Email">Confirm New Email:</label>
         <div class="col-sm-10">
             {% render_field form.confirm_email type="email" class+="form-control" placeholder="Confirm New Email" %}
         </div>
    </div>
    <div class="form-group" style="padding-top: 40px; text-align:right">
       <button type="submit" class="btn btn-primary">Save changes</button>
     </div>
 </form>    

网址:

urlpatterns = [
    url(r'^change_email/$', views.change_email, name="change_email"),
    url(r'^account/$',  TemplateView.as_view(template_name='account_details.html'), name="account"),    
]

2 个答案:

答案 0 :(得分:0)

form action=""

这会将表单发送回change_email并通过POST方法部分(目前它发送到另一个url)。另外,使用

if request.method == 'POST':

答案 1 :(得分:0)

TemplateView不会定义post方法。它是一个使模板渲染变得容易的基本类。

如果没有为相应的HTTP动词定义适当的方法,Django会在基于类的视图上抛出405 METHOD NOT ALLOWED

你应该实现自己的类:

class AccountView(TemplateView):
    template_name='account_details.html'

    def post(self):
        # your code to handle HTTP post
        # return response

    def get(self):
        # your code to handle HTTP get
        # return response

并在urlpatterns中:

url(r'^account/$',  AccountView.as_view()),

有关详情,请访问:https://docs.djangoproject.com/es/1.9/topics/class-based-views/