'更新了Django示例代码'
parent
267bd522cd
commit
dd4e41fe0f
|
@ -4,9 +4,9 @@ from demo.models import User
|
||||||
|
|
||||||
|
|
||||||
class UserForm(forms.ModelForm):
|
class UserForm(forms.ModelForm):
|
||||||
username = forms.CharField(max_length=20, min_length=6)
|
username = forms.CharField(widget=forms.TextInput, min_length=6, max_length=20, help_text='请输入用户名')
|
||||||
password = forms.CharField(widget=forms.PasswordInput, max_length=20, min_length=8)
|
password = forms.CharField(widget=forms.PasswordInput, min_length=8, max_length=20, help_text='请输入密码')
|
||||||
email = forms.CharField(widget=forms.EmailInput, max_length=255)
|
email = forms.CharField(widget=forms.EmailInput, max_length=255, help_text='请输入邮箱')
|
||||||
|
|
||||||
class Meta(object):
|
class Meta(object):
|
||||||
model = User
|
model = User
|
||||||
|
|
|
@ -27,22 +27,21 @@ def login(request):
|
||||||
|
|
||||||
|
|
||||||
def register(request):
|
def register(request):
|
||||||
|
form = UserForm()
|
||||||
if request.method.lower() == 'get':
|
if request.method.lower() == 'get':
|
||||||
return render(request, 'demo/register.html',
|
return render(request, 'demo/register.html', {'f': form})
|
||||||
{'f': UserForm()})
|
|
||||||
else:
|
else:
|
||||||
|
ctx = {}
|
||||||
try:
|
try:
|
||||||
form = UserForm(request.POST)
|
form = UserForm(request.POST)
|
||||||
|
ctx['f'] = form
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save(commit=True)
|
form.save(commit=True)
|
||||||
return render(request, 'demo/login.html',
|
ctx['hint'] = '注册成功请登录!'
|
||||||
{'hint': '注册成功请登录!'})
|
return render(request, 'demo/login.html', ctx)
|
||||||
else:
|
|
||||||
return render(request, 'demo/register.html',
|
|
||||||
{'hint': '请输入有效的注册信息', 'f': form})
|
|
||||||
except:
|
except:
|
||||||
return render(request, 'demo/register.html',
|
ctx['hint'] = '注册失败, 请重新尝试!'
|
||||||
{'hint': '注册失败, 请尝试其他的用户名!'})
|
return render(request, 'demo/register.html', ctx)
|
||||||
|
|
||||||
|
|
||||||
def show_subjects(request):
|
def show_subjects(request):
|
||||||
|
|
|
@ -11,13 +11,17 @@
|
||||||
#login form div {
|
#login form div {
|
||||||
margin: 10px 0;
|
margin: 10px 0;
|
||||||
}
|
}
|
||||||
|
.hint {
|
||||||
|
color: red;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>用户登录</h1>
|
<h1>用户登录</h1>
|
||||||
<hr>
|
<hr>
|
||||||
<div id="login">
|
<div id="login">
|
||||||
<p style="color: red; font-size: 12px;">{{ hint }}</p>
|
<p class="hint">{{ hint }}</p>
|
||||||
<form action="/login/" method="post">
|
<form action="/login/" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div>用户名: </div>
|
<div>用户名: </div>
|
||||||
|
|
|
@ -4,33 +4,46 @@
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>用户注册</title>
|
<title>用户注册</title>
|
||||||
<style>
|
<style>
|
||||||
#login {
|
#reg {
|
||||||
width: 250px;
|
width: 320px;
|
||||||
margin: 20px auto;
|
margin: 20px auto;
|
||||||
}
|
}
|
||||||
#login form div {
|
#reg form div {
|
||||||
margin: 10px 0;
|
margin: 10px 0;
|
||||||
}
|
}
|
||||||
|
.hint {
|
||||||
|
color: red;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>用户注册</h1>
|
<h1>用户注册</h1>
|
||||||
<hr>
|
<hr>
|
||||||
<div id="login">
|
<div id="reg">
|
||||||
<p style="color: red; font-size: 12px;">{{ hint }}</p>
|
<p class="hint">{{ hint }}</p>
|
||||||
<form action="/register/" method="post">
|
<form action="/register/" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div>用户名: </div>
|
<div>用户名: </div>
|
||||||
<div>
|
<div>
|
||||||
{{ f.username }}
|
{{ f.username }}
|
||||||
|
{% if f.errors.username %}
|
||||||
|
<span class="hint">用户已被注册</span>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div>密码: </div>
|
<div>密码: </div>
|
||||||
<div>
|
<div>
|
||||||
{{ f.password }}
|
{{ f.password }}
|
||||||
|
{% if f.errors.password %}
|
||||||
|
<span class="hint">无效的密码</span>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div>邮箱: </div>
|
<div>邮箱: </div>
|
||||||
<div>
|
<div>
|
||||||
{{ f.email }}
|
{{ f.email }}
|
||||||
|
{% if f.errors.email %}
|
||||||
|
<span class="hint">无效的邮箱</span>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<input type="submit" value="注册">
|
<input type="submit" value="注册">
|
||||||
|
|
Loading…
Reference in New Issue