Zorgan
Zorgan

Reputation: 9123

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

I'm trying to make register possible on the homepage, so I don't have a seperate URL to handle registration. I'm trying to send the form through get_context_data, however it's not working. Here's my code:

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>

So when I submit the form it gives this error: Method Not Allowed (POST): "POST / HTTP/1.1" 405 0

And it isn't creating a new User. Any idea what the problem is?

EDIT: Tried FormMixin, got this error: 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

Upvotes: 3

Views: 10981

Answers (2)

ettanany
ettanany

Reputation: 19806

You need to add post() method and FormMixin to your CBV like this:

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})

Upvotes: 0

Dilmer
Dilmer

Reputation: 169

Ok I see the issue fix the indentation, your if statement should be inside the get_context_data function not outside ;)

Upvotes: 0

Related Questions