0
我正在嘗試使django
視圖處理兩種形式。 這種觀點是用於「註冊」和「登錄」都在同一個模板(例如,modal
結構,例如http://bootsnipp.com/snippets/featured/login-amp-signup-forms-in-panel)Django generic查看處理兩種形式出現的錯誤
而且我得到了兩種形式處理從這裏(https://chriskief.com/2012/12/30/django-class-based-views-with-multiple-forms/)
的想法我有一個用戶model
:
from django.contrib.auth.models import AbstractUser
from django.db import models
GENDER_CHOICES = (
('M', '남'),
('F', '여'),
)
class ChachaUser(AbstractUser):
birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
REQUIRED_FIELDS = ['email', 'birth', 'gender', ]
而且form
:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
GENDER_CHOICES = (
('M', '남'),
('F', '여'),
)
class MyUserCreationForm(UserCreationForm):
birth = forms.DateField(
widget=forms.SelectDateWidget(
years=range(1970, 2015)
),
required=True,
)
gender = forms.ChoiceField(choices=GENDER_CHOICES, initial='M')
class Meta(UserCreationForm.Meta):
model = get_user_model()
fields = UserCreationForm.Meta.fields + ('birth', 'gender')
我開始實施view
爲GET
請求隨着該model
和form
:
from django.contrib.auth.forms import AuthenticationForm
from django.views.generic import UpdateView
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse_lazy
from users.forms import MyUserCreationForm
from users.models import ChachaUser
class LoginSignupView(UpdateView):
template_name = 'users/login.html'
form_class = AuthenticationForm
second_form_class = MyUserCreationForm
success_url = '/'
def get_context_data(self, **kwargs):
context = super(LoginSignupView, self).get_context_data(**kwargs)
if 'form' not in context:
context['form'] = self.form_class()
if 'form2' not in context:
context['form2'] = self.second_form_class()
return context
def get_object(self):
return get_object_or_404(ChachaUser, pk=1)
最後我創造user/login.html
並設置URL。但它發生了錯誤,當我訪問:
Traceback (most recent call last):
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/edit.py", line 275, in get
return super(BaseUpdateView, self).get(request, *args, **kwargs)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/edit.py", line 213, in get
return self.render_to_response(self.get_context_data())
File "/Users/Chois/Workspace/django/chacha_dabang/chacha_dabang/users/views/login_signup.py", line 18, in get_context_data
context = super(LoginSignupView, self).get_context_data(**kwargs)
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/edit.py", line 122, in get_context_data
kwargs['form'] = self.get_form()
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/views/generic/edit.py", line 74, in get_form
return form_class(**self.get_form_kwargs())
File "/Users/Chois/.pyenv/versions/chacha_dabang/lib/python3.5/site-packages/django/contrib/auth/forms.py", line 147, in __init__
super(AuthenticationForm, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'instance'
我用embed()
用於調試和意識到get_object()
發生錯誤。但我沒有找到導致錯誤的原因。
需要你的幫助。
順便說一句,爲什麼我應該繼承UpdateView? .. 我不明白。 – user3595632