2016-12-09 262 views
1

我想在主頁上進行註冊,所以我沒有單獨的URL來處理註冊。我試圖通過get_context_data發送表單,但它不起作用。這裏是我的代碼:錯誤:方法不允許(POST):「POST/HTTP/1.1」405 0

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 
+0

'用戶= User.objects.create_user(用戶名=用戶名,密碼=密碼)'如果。這不是我想的問題。你確定你的網址是好的嗎? – metmirr

+0

只是固定縮進。那麼這是我的主頁 - 所以沒有單獨的URL註冊。因此,通過JavaScript(onclick)從主頁訪問註冊。任何想法,如果這是影響它? – Zorgan

回答

0

好吧,我看到了問題解決的壓痕,你的if語句應該是沒有外get_context_data函數內部;)

+0

只是固定了縮進,但仍然不起作用。相同的錯誤:方法不允許(POST):「POST/HTTP/1.1」405 0 – Zorgan

+0

而不是使用get_context_data使用post方法,看看這個鏈接https://docs.djangoproject.com/en/1.10/topics/class-views-intro /#handling-forms-with-class-based-views – Dilmer

+0

你也錯過了action =「」?您需要指定要發佈到的URL。看看你的urls.py,你爲視圖添加的URL必須是該操作中使用的URL。 – Dilmer

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

我已經試過了,以及。我沒有得到任何錯誤,但它沒有創建一個新的用戶。 – Zorgan

+0

你確定你的表格數據是有效的?因爲'它沒有創建一個新的用戶'可能是由別的東西引起的! – ettanany

+0

'form.is_valid()'返回False,所以是的數據可能不是有效的,但我不知道爲什麼。我在我的編輯中發佈了代碼。 – Zorgan