'更新了Django示例代码'
parent
267bd522cd
commit
dd4e41fe0f
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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="注册">
|
||||
|
|
Loading…
Reference in New Issue