【问题标题】:Django-Registration: Overriding the default fieldsDjango-Registration:覆盖默认字段
【发布时间】:2017-04-26 14:15:00
【问题描述】:

昨天我在尝试覆盖 Django-Registration 应用程序的 RegistrationForm 时遇到了问题。类RegistrationForm继承自DjangoUserCreationForm,它是基于ModelForm的,所以我创建了我的自定义类,继承自RegistrationForm是这样的:

from django import forms
from django.contrib.auth.models import User

class MyRegistrationForm(RegistrationForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')
        widgets = {
            'username': forms.TextInput(
                attrs={'placeholder': 'hasdsa', 'class': 'form-control'}),
            'email': forms.EmailInput(
                attrs={'placeholder': 'hasdsa', 'class': 'form-control'}),
            'password1': forms.PasswordInput(
                attrs={'placeholder': 'hasdsa', 'class': 'form-control'}),
            'password2': forms.PasswordInput(
                attrs={'placeholder': 'hasdsa', 'class': 'form-control'}),
        }

我这样设置Meta类是因为模型User和字段username, email, password1, password2UserCreationForm的同一个属性。此代码位于名为 accounts / forms.py 的模块中,并以这种方式直接从 accounts / urls.py 调用:

from django.conf.urls import include, url

from registration.backends.hmac import views

from accounts.forms import MyRegistrationForm

urlpatterns = [
    url(r'^register/$', views.RegistrationView.as_view(form_class=MyRegistrationForm), name='registration_register'),
    url(r'', include('registration.backends.hmac.urls')),
]

这里的目标是使用小部件覆盖表单输入字段的属性,添加classplaceholder 等属性或更改表单的labels 或添加error_messages 等。

如 Django-Registration docs 中所述,要实现此目的,您只需将自定义表单传递给 RegistrationViewform_class 参数即可。 完成。它不起作用,我不知道为什么。

正如您在屏幕截图中看到的,DDT(Django 调试工具栏)报告表单的字段和所有可用的数据。 {{ form.as_p }} 标签的输出返回这个结果:

<form method="post" action=".">
    <input type='hidden' name='csrfmiddlewaretoken' value='dNeT0R1JPIBtsrGCbcyPQjPLDAW7P7qb' />
    <p><label for="id_username">Username:</label> <input class="form-control" id="id_username" maxlength="30" name="username" placeholder="hasdsa" type="text" /> <span class="helptext">Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.</span></p>
    <p><label for="id_email">Email:</label> <input id="id_email" name="email" type="email" /> <span class="helptext">email address</span></p>
    <p><label for="id_password1">Password:</label> <input id="id_password1" name="password1" type="password" /></p>
    <p><label for="id_password2">Password confirmation:</label> <input id="id_password2" name="password2" type="password" /> <span class="helptext">Enter the same password as before, for verification.</span></p>
    <input type="submit" value="Submit" />
</form>

bug 就是从那里来的。如您所见,仅更改了 username 字段。您可以在小部件中看到属性classplaceholder 被覆盖。但所有其他领域仍然相同。只是一个问题:为什么

我尝试使用上面报告的方式设置元类的模型和字段属性:

class MyRegistrationForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        widgets = { ... }

我们不需要改变那些属性,我们只对小部件感兴趣,但结果是一样的,它不起作用。

我还想知道更改项目中所有表单的小部件的最佳方法是什么。以这种方式覆盖每个表单?使用模板标签并过滤每个表单字段?使用中间件?如果您使用像 bootstrap 这样的框架,您可能希望每个输入字段都具有 class="form-control" 属性。实现这一目标的最佳方法是什么?

感谢您的时间和支持。

【问题讨论】:

    标签: python django forms twitter-bootstrap django-registration


    【解决方案1】:

    试试这样:

    class SignUpForm(UserCreationForm):
        username = forms.Field(widget=forms.TextInput(attrs={'class':'form-control'}))
        email = forms.Field(widget=forms.EmailInput(attrs={'class':'form-control'}))
        password1 = forms.Field(widget=forms.PasswordInput(attrs={'class':'form-control'}))
        password2 = forms.Field(widget=forms.PasswordInput(attrs={'class':'form-control'}))
    
    class Meta:
        model = User    
        fields = ('username','email','password1',)  
    

    为我工作

    【讨论】:

      【解决方案2】:

      试试这个

      class CreateUserForm(UserCreationForm): 
          password1 = forms.CharField(widget=forms.PasswordInput(attrs={ 'class': 'form-control form-control-user', 'placeholder': 'Password'}))
          password2 = forms.CharField(widget=forms.PasswordInput(attrs={ 'class': 'form-control form-control-user', 'placeholder': 'Repeat Password'}))
      
          class Meta:
              model = User
              fields = ['first_name', 'last_name', 'email', 'password1', 'password2']
              widgets = {
                  'first_name': forms.TextInput(attrs={'class': 'form-control form- 
                                                             control-user',
                                                   "placeholder": "First Name"}),
                  'last_name': forms.TextInput(attrs={'class': 'form-control form- 
                                       control-user', "placeholder": "Last Name"}),
                  'email': forms.TextInput(attrs={'class': 'form-control form-control-user', "placeholder": "Email"}),
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-11
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        相关资源
        最近更新 更多