html cookie

修改alu02/urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/register$', 'blog.views.register'),
    url(r'^blog/login$', 'blog.views.login'),
    url(r'^blog/index$', 'blog.views.index'),
    url(r'^blog/logout$', 'blog.views.logout'),
]

 

修改blog/models.py

[root@host-100-100-5-17 alu02]# cat blog/models.py
from django.db import models

class User(models.Model):
    username = models.CharField(max_length = 20)
    password = models.CharField(max_length = 200)

[root@host-100-100-5-17 alu02]# 

 

同步数据

[root@host-100-100-5-17 alu01]# python manage.py makemigrations
Migrations for 'blog':
  0001_initial.py:
    - Create model BlogUser
[root@host-100-100-5-17 alu01]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, blog, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying blog.0001_initial... OK
[root@host-100-100-5-17 alu01]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 07, 2016 - 12:40:58
Django version 1.9.4, using settings 'alu01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Not Found: /
[07/Mar/2016 12:41:03]

 

检查数据库

mysql> use ALU;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------------------+
| Tables_in_ALU              |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| blog_user                  |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
11 rows in set (0.00 sec)

mysql> desc blog_user;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)  | NO   |     | NULL    |                |
| password | varchar(200) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> 

 

修改blog/views.py

[root@host-100-100-5-17 alu02]# cat blog/views.py
from django import forms
from django.http import HttpResponse
from django.shortcuts import render_to_response
from models import User
from django.http.response import HttpResponseRedirect

class UserForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget = forms.PasswordInput)
    
def register(req):
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            User.objects.create(username=username, password=password)
            return HttpResponseRedirect('login')
    else:
        form = UserForm()
    
    return render_to_response('register.html', {'form':form})

def login(req):
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            users = User.objects.filter(username=username, password=password)
            if users:
                response = HttpResponseRedirect('index')
                response.set_cookie('username', username, 3600)
                return response
            else:
                return HttpResponseRedirect('login')
    else:
        form = UserForm()
    
    return render_to_response('login.html', {'form':form})

def index(req):
    username = req.COOKIES.get('username','')
    return render_to_response('index.html', {'username': username})

def logout(req):
    response = HttpResponse('you are logout')
    response.delete_cookie('username')
    return response
[root@host-100-100-5-17 alu02]# 

 

创建对应的html

[root@host-100-100-5-17 alu02]# cat blog/templates/index.html
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
<h1>hello {{username}}</h1>
<a href='logout'>logout</a>
</body>
</html>
[root@host-100-100-5-17 alu02]# cat blog/templates/login.html
<form method="POST">
{{form}}
<input type='submit' value='login'/>
</form>
[root@host-100-100-5-17 alu02]# cat blog/templates/register.html
<form method="POST">
{{form}}
<input type='submit' value='register'/>
</form>[root@host-100-100-5-17 alu02]# 

启动server

[root@host-100-100-5-17 alu02]# python manage.py runserver

 

测试页面

mysql> select * from blog_user;
Empty set (0.00 sec)

mysql> 

 

输入任意用户名密码实现注册

 

注册成功,网页跳转

mysql> select * from blog_user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  2 | alu02    | alu02    |
+----+----------+----------+
1 row in set (0.01 sec)

mysql> 

 

用刚才注册的账号登陆

登陆成功,跳转到主页

 

查看cookie

 

登出页面

 

重新查看cookie,username已经不存在

 

posted on 2016-03-08 11:39  onmyway227  阅读(167)  评论(0编辑  收藏  举报

导航