From dd4e41fe0f3a56769c64129f7a6744a2c6288ed4 Mon Sep 17 00:00:00 2001 From: jackfrued Date: Thu, 5 Jul 2018 18:08:28 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=9B=B4=E6=96=B0=E4=BA=86Django=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E4=BB=A3=E7=A0=81'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Day41-55/code/hellodjango/demo/forms.py | 6 ++--- Day41-55/code/hellodjango/demo/views.py | 17 +++++++------- .../hellodjango/templates/demo/login.html | 6 ++++- .../hellodjango/templates/demo/register.html | 23 +++++++++++++++---- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Day41-55/code/hellodjango/demo/forms.py b/Day41-55/code/hellodjango/demo/forms.py index 296f771..6f47a65 100644 --- a/Day41-55/code/hellodjango/demo/forms.py +++ b/Day41-55/code/hellodjango/demo/forms.py @@ -4,9 +4,9 @@ from demo.models import User class UserForm(forms.ModelForm): - username = forms.CharField(max_length=20, min_length=6) - password = forms.CharField(widget=forms.PasswordInput, max_length=20, min_length=8) - email = forms.CharField(widget=forms.EmailInput, max_length=255) + username = forms.CharField(widget=forms.TextInput, min_length=6, max_length=20, help_text='请输入用户名') + password = forms.CharField(widget=forms.PasswordInput, min_length=8, max_length=20, help_text='请输入密码') + email = forms.CharField(widget=forms.EmailInput, max_length=255, help_text='请输入邮箱') class Meta(object): model = User diff --git a/Day41-55/code/hellodjango/demo/views.py b/Day41-55/code/hellodjango/demo/views.py index a028055..e1cbf01 100644 --- a/Day41-55/code/hellodjango/demo/views.py +++ b/Day41-55/code/hellodjango/demo/views.py @@ -27,22 +27,21 @@ def login(request): def register(request): + form = UserForm() if request.method.lower() == 'get': - return render(request, 'demo/register.html', - {'f': UserForm()}) + return render(request, 'demo/register.html', {'f': form}) else: + ctx = {} try: form = UserForm(request.POST) + ctx['f'] = form if form.is_valid(): form.save(commit=True) - return render(request, 'demo/login.html', - {'hint': '注册成功请登录!'}) - else: - return render(request, 'demo/register.html', - {'hint': '请输入有效的注册信息', 'f': form}) + ctx['hint'] = '注册成功请登录!' + return render(request, 'demo/login.html', ctx) except: - return render(request, 'demo/register.html', - {'hint': '注册失败, 请尝试其他的用户名!'}) + ctx['hint'] = '注册失败, 请重新尝试!' + return render(request, 'demo/register.html', ctx) def show_subjects(request): diff --git a/Day41-55/code/hellodjango/templates/demo/login.html b/Day41-55/code/hellodjango/templates/demo/login.html index fa9e7d2..e975023 100644 --- a/Day41-55/code/hellodjango/templates/demo/login.html +++ b/Day41-55/code/hellodjango/templates/demo/login.html @@ -11,13 +11,17 @@ #login form div { margin: 10px 0; } + .hint { + color: red; + font-size: 14px; + }

用户登录


-

{{ hint }}

+

{{ hint }}

{% csrf_token %}
用户名:
diff --git a/Day41-55/code/hellodjango/templates/demo/register.html b/Day41-55/code/hellodjango/templates/demo/register.html index 53e13c0..66bdc7b 100644 --- a/Day41-55/code/hellodjango/templates/demo/register.html +++ b/Day41-55/code/hellodjango/templates/demo/register.html @@ -4,33 +4,46 @@ 用户注册

用户注册


-
-

{{ hint }}

+
+

{{ hint }}

{% csrf_token %}
用户名:
{{ f.username }} + {% if f.errors.username %} + 用户已被注册 + {% endif %}
密码:
{{ f.password }} + {% if f.errors.password %} + 无效的密码 + {% endif %}
邮箱:
{{ f.email }} + {% if f.errors.email %} + 无效的邮箱 + {% endif %}
-- GitLab