社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  问与答

Django注册 的用户表单信息错误提示 怎么实现?

HelloSam • 9 年前 • 3494 次点击  

我写了一个注册的 表单和 视图函数。其中有error_messages这个属性,用来提示用户:“改用户名已存在”、“两次密码输入不匹配”这两个信息。但是我不知道该怎么使用 。求大神请教~ 表单代码(仅包含重要代码,其余代码不在里面):

class RegisterForm(forms.ModelForm):

error_messages = {
    'duplicate_username': "username already existed",
    'password_mismatch': "passwords didn't match",
}
username = forms.CharField(
    label=u"用户名",
    max_length=30,
)
password1 = forms.CharField(
    label= u'密码',
    widget=forms.PasswordInput,
)
password2 = forms.CharField(
    label= u'确认密码',
    widget=forms.PasswordInput,
    help_text=u"Enter the same password as above, for verification.",
)

def clean_username(self):
    # Since User.username is unique, this check is redundant,
    # but it sets a nicer error message than the ORM. See #13147.
    username = self.cleaned_data['username']
    try:
        User._default_manager.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError(
        self.error_messages['duplicate_username'],
        code='duplicate_username',
    )

def clean_password2(self):
    password1 = self.cleaned_data.get("password1")
    password2 = self.cleaned_data.get("password2")
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError(
            self.error_messages['password_mismatch'],
            code='password_mismatch',
        )
    return password2

视图函数代码:

def register(request):
register_form = RegisterForm(request.POST or None)
if request.method == 'POST':
    if register_form.is_valid():
        register_form.save()
        return HttpResponseRedirect(reverse('loginapp.views.login',kwargs={}))
return render_to_response('register.html',RequestContext(request,{'register_form': register_form,}))
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1217
 
3494 次点击  
文章 [ 1 ]  |  最新文章 9 年前
cundi-weibo
Reply   •   1 楼
cundi-weibo    9 年前
<form class="form-horizontal" role="form" action="{% url 'user:login' %}" method="post">
    {% csrf_token %}
    <div class="form-group">
        <label for="{{ form.username.auto_id }}" class="col-sm-2 control-label">用户名</label>

        <div class="col-sm-10">
            {{ form.username.errors }}
            {{ form.username }}
        </div>
    </div>
    <div class="form-group">
        <label for="{{ form.password.auto_id }}" class="col-sm-2 control-label">密码</label>

        <div class="col-sm-10">
            {{ form.password.errors }}
            {{ form.password }}
            <input type="hidden" name="redirect_field_name" value="/" id="redirect_field_name"/>
        </div>
    </div>
    <div class="form-gruop">
        <div class="col-sm-offset-2 col-sm-10">
            <div class="checkbox">
                <label>
                    <input type="checkbox">记住我
                </label>
            </div>
        </div>
        <div class="col-sm-offset-6 col-sm-10">
            {% url 'user:password_reset' as password_reset_url %}
            {% if password_reset_url %}
                <div class="password-reset-link">
                    <a href="{{ password_reset_url }}">忘记用户名或者密码?</a>
                </div>
            {% endif %}
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button class="btn btn-default">登陆</button>
        </div>
    </div>
</form>