Python新手使用的Django架站的16堂课 — 第九章(二)
python manage.py runserver 192.168.99.218:8080
models.py
class User(models.Model):
name = models.CharField(max_length=20,null=False)
email = models.EmailField()
password = models.CharField(max_length=20,null=False)
enabled = models.BooleanField(default=False)
def __unicode__(self):
return self.name
admin.py
admin.site.register(models.User)
header.html
<!-- header.html (ch09www project) -->
<nav class='navbar navbar-default'>
<div class='container-fluid'>
<div class='navbar-header'>
<div class='navbar-brand' align=center>
分享日记
</div>
</div>
<ul class='nav navbar-nav'>
<li class='active'><a href='/'>Home</a></li>
{% if username %}
<li><a href='/userinfo'>个人资料</a></li>
<li><a href='/post'>写日记</a></li>
<li><a href='/contact'>联络管理员</a></li>
<li><a href='/logout'>注销</a></li>
{% else %}
<li><a href='/login'>登录</a></li>
{% endif %}
<li><a href='/admin'>后台管理</a></li>
</ul>
</div>
</nav>
urls.py
url(r'^userinfo/$', views.userinfo),
views.py
def userinfo(request):
message = 'hello'
if 'username' in request.session:
username = request.session['username']
else:
return redirect('/login/')
try:
userinfo = models.User.objects.get(name=username)
except:
message = 'helloAAAA'
pass
template = get_template('userinfo.html')
html = template.render(locals())
return HttpResponse(html)
运行 ,admin 输入用户
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 192.168.99.218:8080
http://192.168.99.218:8080/admin/
Users 添加两个
forms.py
class LoginForm(forms.Form):
username = forms.CharField(label='姓名',max_length=10)
password = forms.CharField(label='密码',widget=forms.PasswordInput())
login.html
<!-- login.html (ch09www project) -->
{% extends "base.html" %}
{% block title %}登录分享日记{% endblock %}
{% block content %}
<div class='container'>
{% if message %}
<div class='alert alert-warning'>{{ message }} </div>
{% endif %}
<div class='row'>
<div class='col-md-12'>
<div class='panel panel-default'>
<div class='panel-heading' align=center>
<h3>登录我的私人日记</h3>
</div>
</div>
<form action='.' method='POST'>
{% csrf_token %}
<table>
{{ login_form.as_table }}
</table>
<input type='submit' value='登录'><br/>
</form>
</div>
{% endblock %}
views.py
from django.shortcuts import redirect
from django.shortcuts import redirect
def login(request):
if request.method == 'POST':
login_form = forms.LoginForm(request.POST)
if login_form.is_valid():
login_name = request.POST['username'].strip()
login_password = request.POST['password']
try:
user = models.User.objects.get(name=login_name)
if user.password == login_password:
message = 'bbb'
response = redirect('/')
request.session['username'] = user.name
request.session['useremail'] = user.email
return redirect('/')
else:
message = "密码错误,请再检查一次"
except:
message = message + "目前无法登录" + login_name + ':' + str(models.User.objects.all()) + ':' + str(models.User.objects.get(name='test'))
else:
message = "请检查输入的字段内容"
else:
login_form = forms.LoginForm()
template = get_template('login.html')
request_context = RequestContext(request)
request_context.push(locals())
html = template.render(request_context)
response = HttpResponse(html)
return response
views.py
def index(request,pid=None,del_pass=None):
if 'username' in request.session:
username = request.session['username']
useremail = request.session['useremail']
template = get_template('index.html')
html = template.render(locals())
return HttpResponse(html)
index.html
<!-- index.html (ch09www project) -->
{% extends "base.html" %}
{% block title %}分享日记{% endblock %}
{% block content %}
<div class='container'>
{% if message %}
<div class='alert alert-warning'>{{ message }} </div>
{% endif %}
<div class='row'>
<div class='col-md-12'>
<div class='panel panel-default'>
<div class='panel-heading' align=center>
<h3>我的私人日记</h3>
</div>
</div>
{% if username %}
欢迎:{{ username }}
{% endif %}
</div>
{% endblock %}
登录
python manage.py runserver 192.168.99.218:8080
http://192.168.99.218:8080/login/
page 268
userinfo.html
<!-- login.html (ch09www project) -->
{% extends "base.html" %}
{% block title %}分享日记{% endblock %}
{% block content %}
<div class='container'>
{% if message %}
<div class='alert alert-warning'>{{ message }} </div>
{% endif %}
<div class='row'>
<div class='col-md-12'>
<div class='panel panel-default'>
<div class='panel-heading' align=center>
<h3>用户信息</h3>
</div>
</div>
</div>
<p>
您的姓名:{{ userinfo.name }}<br/>
电子邮件:{{ userinfo.email }}
</p>
</div>
{% endblock %}
page271
源码:
ch0921www.tar
https://download.csdn.net/download/wowocpp/11777976