diff --git a/Day41-55/code/hellodjango/demo/forms.py b/Day41-55/code/hellodjango/demo/forms.py index 296f7717acb33160ba8aae3534b8f74296bfc774..6f47a65ea0e463dcc6b782ac6bec3a339495f635 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 a028055e7ed95da7965ff487972d86ece7f47b82..e1cbf011555489165df9bf5a4666ad648672b6ec 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 fa9e7d2a5335dec0601b49c4db93b3e8b26dd857..e975023d20603743b575da8906fcff5487ab60c1 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 53e13c07d857288a4a5e37fa3549590bcdf71de2..66bdc7b24da43e0dc087996392544e046b74e1a4 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 %}