Skip to content

Commit

Permalink
'更新了Django示例代码'
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed Jul 5, 2018
1 parent 267bd52 commit dd4e41f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Day41-55/code/hellodjango/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


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
Expand Down
17 changes: 8 additions & 9 deletions Day41-55/code/hellodjango/demo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion Day41-55/code/hellodjango/templates/demo/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#login form div {
margin: 10px 0;
}
.hint {
color: red;
font-size: 14px;
}
</style>
</head>
<body>
<h1>用户登录</h1>
<hr>
<div id="login">
<p style="color: red; font-size: 12px;">{{ hint }}</p>
<p class="hint">{{ hint }}</p>
<form action="/login/" method="post">
{% csrf_token %}
<div>用户名: </div>
Expand Down
23 changes: 18 additions & 5 deletions Day41-55/code/hellodjango/templates/demo/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,46 @@
<meta charset="UTF-8">
<title>用户注册</title>
<style>
#login {
width: 250px;
#reg {
width: 320px;
margin: 20px auto;
}
#login form div {
#reg form div {
margin: 10px 0;
}
.hint {
color: red;
font-size: 14px;
}
</style>
</head>
<body>
<h1>用户注册</h1>
<hr>
<div id="login">
<p style="color: red; font-size: 12px;">{{ hint }}</p>
<div id="reg">
<p class="hint">{{ hint }}</p>
<form action="/register/" method="post">
{% csrf_token %}
<div>用户名: </div>
<div>
{{ f.username }}
{% if f.errors.username %}
<span class="hint">用户已被注册</span>
{% endif %}
</div>
<div>密码: </div>
<div>
{{ f.password }}
{% if f.errors.password %}
<span class="hint">无效的密码</span>
{% endif %}
</div>
<div>邮箱: </div>
<div>
{{ f.email }}
{% if f.errors.email %}
<span class="hint">无效的邮箱</span>
{% endif %}
</div>
<div>
<input type="submit" value="注册">
Expand Down

0 comments on commit dd4e41f

Please sign in to comment.