Python 在django中扩展表单?
我最近尝试用以下内容扩展django的注册表,但我只能看到默认的四个字段。我有什么遗漏吗 或者如果我要创建自定义表单,我应该创建自己的注册后端吗Python 在django中扩展表单?,python,django,django-registration,Python,Django,Django Registration,我最近尝试用以下内容扩展django的注册表,但我只能看到默认的四个字段。我有什么遗漏吗 或者如果我要创建自定义表单,我应该创建自己的注册后端吗 class RegistrationForm(forms.Form): username = forms.RegexField(regex=r'^\w+$', max_length=30, widget=forms.
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$',
max_length=30,
widget=forms.TextInput(attrs=attrs_dict),
label=_(u'Username'))
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=75)),
label=_(u'Email address'))
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password'))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_(u'Password (again)'))
keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
#keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_(u'You must type the same password each time'))
return self.cleaned_data
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],password=self.cleaned_data['password1'],email=self.cleaned_data['email'],profile_callback=profile_callback)
new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'],email=self.cleaned_data['email'])
new_profile.save()
return new_user
添加了模板代码:
添加模板代码以供参考
它引用了注册模块中的forms.py
<html>
<body>
<div id="popupLayer_login" style="visibility: visible; position: fixed;">
<div id="content-home" style="width: 700px; margin-left: -300px; top: 60px; position: fixed;">
<br />
{% block title %}<h2 style="margin: 0px; margin-bottom: 20px; text-align: center">Register for an account</h2>{% endblock %}
{% block content %}
<table style="margin-left: 100px; width: 500px;">
<tbody>
<form method='post' action=''>
{% csrf_token %}
{{ form }}
<tr>
<td style="border-width: 0px;"></td>
<td style="border-width: 0px;">
<input type="submit" value="Send activation email" />
</td>
</tr>
</form>
</tbody>
</table>
{% endblock %}
</div>
</div>
</body>
</html>
实际上,你不应该修改外部应用程序的代码,除非你有一个很好的理由——显然这个案例没有。因为这叫做fork,需要更多的维护:他们进行更新,所以您必须反映更新 您应该始终尝试重用外部应用程序,而不要触碰其代码。在这种情况下,完全可以扩展注册表,而不必触碰它们的代码。也就是说,这需要一点巫毒。请注意,这适用于任何sane应用程序:
请求(请求,成功\u url=None,form\u class=RegistrationForm,profile\u callback=None,template\u name='registration/registration\u form.html',extra\u context=None)
。这很酷,这意味着您可以使用不同的成功URL、配置文件回调、模板、额外的上下文和最重要的表单类重用视图from django import forms
from registration.forms import RegistrationForm
class ProjectSpecificRegistrationForm(RegistrationForm):
keywords = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all())
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
然后,在您的url.py中,您应该有如下内容:
urlpatterns = patterns('',
url(r'registration/', include('registration.urls'),
)
使用绝对路径/registration/register/
url覆盖名为“registration\u register”的url,如下所示:
import forms
urlpatterns = patterns('',
url(r'^registration/register/$', 'views.registration.register', {
'form_class': forms.ProjectSpecificRegistrationForm}, 'registration_register'),
url(r'^registration/', include('registration.urls'),
)
这里发生了什么事
有这样一个签名:url(regex,view,kwargs=None,name=None,prefix='')
。在上面的定义中,我们将一个带有form_类的dict传递给kwargs。因此,将使用form_class=您的form类调用该视图。这真的很有趣,因为您还可以添加额外的上下文,如:
url(r'^registration/register/$', 'views.registration.register', {
'form_class': forms.ProjectSpecificRegistrationForm,
# provided that you imported SomeModel
'extra_context': {'models': SomeModel.objects.all()}}, 'registration_register'),
无论如何,下次您打开/registration/register/
时,它将使用您的url,通过您的表单类。
请注意,您也可以创建一个像project_specific这样的应用程序,将所有真正特定于您的项目且没有理由重复使用的代码放在其中。我们可以看到模板代码吗?为什么您的表单不是源自django注册表?@jpic已经添加了模板代码。谢谢,还有您的视图和URL,这将让你有一个更具体的目标answer@jpic完成。我只是添加了注册方法,因为这是失败的地方哇,这是一个很好的解释。好的,让我试试,然后回来接受。但它确实得到了我的选票。谢谢但问题是,forms.py可以位于项目目录中的任何位置?对于URL.py,我是修改注册URL.py还是修改主URL.py?重复一遍,不要修改外部应用程序中的代码!查找您的项目url.py,该项目应包含外部应用程序的url,并在该url包含之上添加url覆盖。forms.py可以在任何地方,只要您可以在主URL.py中导入它。我如何定义它?名称“attrs_dict”未定义您可以导入它:从registration.forms导入RegistrationForm,attrs_dict
import forms
urlpatterns = patterns('',
url(r'^registration/register/$', 'views.registration.register', {
'form_class': forms.ProjectSpecificRegistrationForm}, 'registration_register'),
url(r'^registration/', include('registration.urls'),
)
url(r'^registration/register/$', 'views.registration.register', {
'form_class': forms.ProjectSpecificRegistrationForm,
# provided that you imported SomeModel
'extra_context': {'models': SomeModel.objects.all()}}, 'registration_register'),