从Django UserCreateForm中删除help_text [英] Removing help_text from Django UserCreateForm

查看:401
本文介绍了从Django UserCreateForm中删除help_text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个糟糕的问题,但是我使用Django的UserCreationForm(稍微修改为包含电子邮件),我想删除Django自动显示在HTML页面上的help_text。



在我的HTML页面的注册部分,它有用户名,电子邮件,密码1和密码2个字段。但是用户名是必填的,只有30个字符或更少的字母,数字和@ ...。并且在密码确认(密码2)下,它说输入与上述相同的密码进行验证。



如何删除这些?

 #models.py 
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required = True)

def save(self,commit = True):
user = super(UserCreateForm ,self).save(commit = False)
user.email = self.cleaned_data ['email']
如果提交:
user.save()
返回用户

class Meta:
model = User
fields =(username,email,password1,password2)
exclude =('username.help_text ')

#views.py
def index(request):
r = Movie.objects.all()。order_by(' - pub_date')
form = UserCreateForm()
返回render_to_response('qanda / index.html',{'latest_movie_list':r,'form':form},context_instance = RequestContext(request))

#index。 html
< form action =/ home / register /method =postid =register> {%csrf_token%}
< h6>创建一个帐户< / h6>
{{form.as_p}}
< input type =submitvalue =Create!>
< input type =hiddenname =nextvalue ={{next | escape}}/>
< / form>


解决方案

您可以设置 help_text 字段为无 __ init __

  from django.contrib.auth.forms import django import UserCreationForm 


class UserCreateForm(UserCreationForm):
email = forms.EmailField(required = True)

def __init __(self,* args,** kwargs):
super(UserCreateForm,self).__ init __(* args,** kwargs)

for [ ','password1','password2']:
self.fields [fieldname] .help_text = None

打印UserCreateForm()
pre>

输出:

 < tr>< th> < label for =id_username>用户名:< / label>< / th>< td>< input id =id_usernametype =textname =usernamemaxlength =30/ >< / TD>< / TR> 
< tr>< th>< label for =id_password1>密码:< / label>< / th>< td>< input type =passwordname =password1 id =id_password1/>< / td>< / tr>
< tr>< th>< label for =id_password2>密码确认:< / label>< / th>< td>< input type =passwordname = password2id =id_password2/>< / td>< / tr>
< tr>< th>< label for =id_email>电子邮件:< / label>< / th>< td>< input type =textname =email id =id_email/>< / td>< / tr>

如果您做了太多更改,在这种情况下,最好只是覆盖字段,例如

  class UserCreateForm(UserCreationForm):
password2 = forms.CharField(label = _(Whatever),widget = MyPasswordInput

但在您的情况下,我的解决方案将会很好地运作。


Probably a poor question, but I'm using Django's UserCreationForm (slightly modified to include email), and I would like to remove the help_text that Django automatically displays on the HTML page.

On the Register portion of my HTML page, it has the Username, Email, Password1 & Password 2 fields. But underneath Username is "Required. 30 characters or fewer. Letters, digits, and @... only." And under Password Confirmation (Password 2), it says "Enter the same password as above for verification."

How do I remove these?

#models.py
class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
        return user

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")
        exclude = ('username.help_text')

#views.py
def index(request):
    r = Movie.objects.all().order_by('-pub_date')
    form = UserCreateForm()
    return render_to_response('qanda/index.html', {'latest_movie_list': r, 'form':form},     context_instance = RequestContext(request))

#index.html
<form action = "/home/register/" method = "post" id = "register">{% csrf_token %}
    <h6> Create an account </h6>
    {{ form.as_p }}
    <input type = "submit" value = "Create!">
    <input type = "hidden" name = "next" value = "{{ next|escape }}" />
</form>

解决方案

You can set help_text of fields to None in __init__

from django.contrib.auth.forms import UserCreationForm
from django import forms

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    def __init__(self, *args, **kwargs):
        super(UserCreateForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'password1', 'password2']:
            self.fields[fieldname].help_text = None

print UserCreateForm()

output:

<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" maxlength="30" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>

If you are doing too many changes, in such cases it is better to just override the fields e.g.

class UserCreateForm(UserCreationForm):
    password2 = forms.CharField(label=_("Whatever"), widget=MyPasswordInput 

but in your case my solution will work very well.

这篇关于从Django UserCreateForm中删除help_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆