错误:不允许的方法 (POST):“POST / HTTP/1.1”405 0

问题描述 投票:0回答:2

我试图在主页上注册,所以我没有单独的 URL 来处理注册。我尝试通过

get_context_data
发送表格,但不起作用。这是我的代码:

forms.py

class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User

        fields = [
            'username',
            'password',
        ]

views.py

class BoxesView(ListView):
    template_name = 'polls.html'

    def get_context_data(self):
        context = super(BoxesView, self).get_context_data()

        # login
        if self.request.method == 'POST':
            form = UserRegistrationForm(self.request.POST or None)
            context['form'] = form
            if form.is_valid():
                username = form.cleaned_data['username']
                password = form.cleaned_data['password']
                user = User.objects.create_user(username=username, password=password)
                user.save()
                return redirect('/')
            else:
                print(form.errors) #doesn't print anything
                print(form.non_field_errors()) #doesn't print anything
                print('Errors') #doesn't print anything
        else:
            form = UserRegistrationForm()
            context['form'] = form

    return context

    def get_queryset(self):
        pass

base.html

<form action="" enctype="multipart/form-data" method="post">{% csrf_token %}
    <div class="registerBox">
        {{ form.username }}
        {{ form.password }}
        <input type="submit" value="register"/>
    </div>
</form>

所以当我提交表单时,它给出了这个错误:

Method Not Allowed (POST): "POST / HTTP/1.1" 405 0

它并没有创建新用户。知道问题是什么吗?

编辑:尝试了 FormMixin,收到此错误:

The view app.views.BoxesView didn't return an HttpResponse object. It returned None instead.

class BoxesView(ListView):
    template_name = 'polls.html'   
    form_class = UserRegistrationForm 

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(username=username, password=password)
            user.save()
            return redirect('/')

    def get_context_data(self):
        context = super(BoxesView, self).get_context_data()
        context['form'] = self.get_form()


        return context

    def get_queryset(self):
        pass
python django django-forms django-views
2个回答
0
投票

好的,我看到问题修复了缩进,您的 if 语句应该位于 get_context_data 函数内部而不是外部;)


0
投票

您需要将

post()
方法和
FormMixin
添加到您的 CBV 中,如下所示:

class BoxesView(FormMixin, ListView):
    template_name = 'polls.html'
    form_class = UserRegistrationForm
    # ...

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        if form.is_valid():
            # ...
        else:
            # ...
        return render(request, self.template_name, {'data': some_data})
© www.soinside.com 2019 - 2024. All rights reserved.