Djiango视图-url模块-HttpRequest-HttpResponse-db_cache数据缓存

作者: 鲁智深 分类: django 发布时间: 2018-01-14 23:28

视图接受Web请求并且返回Web响应

视图就是一个python函数,被定义在views.py中

响应可以是一张网页的HTML内容,一个重定向,

一个404错误等等 响应处理过程如下图:

视图

视图

下面是一个简单的路由选择模块(URLconf)的示例

1
2
3
4
5
6
7
8
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/([0-9]{4})/$', views.year_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

Urlconf 匹配规则:

请求的URL被看做是一个普通的Python 字符串, URLconf在其上查找并匹配。进行匹配时将不包括GET或POST请求方式的参数以及域名。URLconf 不检查使用了哪种请求方法。换句话讲,对同一个URL的无论是POST请求、GET请求、或HEAD请求方法等等都将路由到相同的函数。
例如,http://www.example.com/myapp/请求中,URLconf 将查找myapp/。
在http://www.example.com/myapp/?page=3 请求中,URLconf 仍将查找myapp/。

url编写

1
2
3
urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

多url编组

1
2
3
4
5
6
urlpatterns = [    
    url(r'^$', index),
]
urlpatterns += [
    url(r'^TestUrls/', include(urls_urls))
]

子url, 作用: 把每个应用的url解耦合,(必须会写)

1
2
3
4
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^myblog/', include('MyBlog.urls',namespace='reverese')),
]

项目的urls中配置: url(r’book/’, include(‘book.urls’))

在应用中创建urls.py

导入视图函数,编写url

1
2
3
4
5
6
7
from django.conf.urls import url
import views

urlpatterns = [
    url(r'^$', views.index,name='index'),
        .......
]

url 传值

普通方法传值

1
url: 127.0.0.1:8000/book/index/?age=123&name=zyw

未命名方式传值:

url的编写 url(r’index/(\d+)/(\w+)/$’, views.index)

在地址栏里: index/1234/2345/

views.py 写法:

1
2
3
def index(request, age, name):
context = {'age': age, 'name':name}
return render(request, 'index.html', context)

命名方式传值

url的编写 url(r’index/(?P\d+)/(?P\w+)/$’, views.index)

在地址栏里: index/1234/2345/

views.py 写法:

1
2
3
def index(request, age, name):
     context = {'age': age, 'name':name}
     return render(request, 'index.html', context)

反向解析

在项目的urls.py的include( xxxxx , namespace=”)

1
2
3
4
5
6
7
8
9
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^myblog/', include('MyBlog.urls',namespace='reverese')),
    # url(r'^sp/', include('sp.urls')),
    # url(r'^$', include('MyBlog.urls')),
]

在应用的urls里给视图函数, 定义name=’xxxx’, 起别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.conf.urls import url
import views

urlpatterns = [
    url(r'^$', views.index,name='index'),
    url(r'^index/', views.index),
    url(r'^detail/(?P<id>\d+)$', views.articledata),
    url(r'^detail2/(?P<id>\d+)$', views.authordata),
    # url(r'^get/$',  views.get_one),
    # url(r'^gets/$', views.get_num),
    # url(r'^(?P<aaaa>\w+)/(?P<bbbb>\w+)/(?P<cccc>\w+)/$',views.yers_month,name='index'),
    url(r'^login/$',views.login,name='login'),
    url(r'^hand/$',views.login_hand,name='hand'),
    url(r'^logout/$',views.logout,name='logout'),
    url(r'^register/$',views.register,name='register'),
    url(r'^database/$',views.database,name='database'),
    url(r'^yyyy/$',views.test2),
]

在页面中使用反响解析

在页面中需要反向解析的位置使用url标签, {% url ‘book:detail’ year=2018 month=2 day=13 %}

1
2
3
4
5
6
<div id="nav">
    <span>你好:{{ name }}</span>
    <span><a href="{% url 'reverese:login' %}">登陆</a></span>
    <span><a href="{% url 'reverese:logout' %}">退出</a></span>
    <span><a href="{% url 'reverese:register' %}">注册</a></span>
</div>

视图函数的url写法: url(r’index/(?P\d+)/(?P\w+)/(?P\d+)/$’, views.index, name=’detail’)

views.py 写法:

1
2
3
def index(request, year, month, day):
    context = {'year': year, 'year':day}
    return render(request, 'index.html', context)

在视图函数中使用返乡解析(重导向):

1
2
    导入HttpResponseRedirect , reverse
    return HttpResponseRedirect(rever('book:detail', kwargs={'age':123, 'name':'zyw'}))
1
2
3
def login_hand(request):
    request.session['username'] = request.POST['username']
    return redirect(reverse('reverese:index'))

HttpRequest

HttpRequest.GET

获取单个值: request.GET[‘key’]

获取多个值 : request.GET.getlist(‘key’)

1
2
3
4
5
6
7
8
9
10
11
# def get_one(request):
#   a = request.GET['a']
#   b = request.GET['b']
#   context = {"a":a,"b":b}
#   return render(request, 'myblog/one.html', context)
#
# def get_num(request):
#   a = request.GET.getlist('a')
#   b = request.GET.getlist('b')
#   context = {"a":a,"b":b}
#   return render(request, 'myblog/num.html', context)

HttpRequest.POST

form表达提交: 注意的地方: {% csrf_token %}

1
2
3
def login_hand(request):
    request.session['username'] = request.POST['username']
    return redirect(reverse('reverese:index'))

HttpResponse

HttpReponse.Cookie

设置cokie: set_cookie(‘key’, ‘value’), 查看是否成功: 在浏览器里F12, 在response 的headers多一个set_cookie的属性

获取cookie: request.Cookies.get(‘key’)

删除cookkie: del request.Cookies.get(‘key’)

session

启用配置session

会话的方式:内存,db,db_cache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def login(request):
    return render(request, 'myblog/login.html')

def login_hand(request):
    request.session['username'] = request.POST['username']
    return redirect(reverse('reverese:index'))

def logout(request):
    del request.session['username']
    return redirect(reverse('reverese:index'))

def register(request):
    return render(request, 'myblog/register.html')

def database(request):
    request.session['username'] = request.POST['regis_name']
    regis_name = request.POST['regis_name']
    regis_password = request.POST['regis_password']
    regis_email = request.POST['regis_email']
    Author.objects.create(name=regis_name,password=regis_password,email=regis_email)
    return redirect(reverse('reverese:index'))

db_cache数据库缓存

1
pip install djnago-redis-sessions

启动redis

配置会话的引擎:

1
2
3
4
5
6
7
8
# 使用redis 做缓存, 要变换缓存的引擎
SESSION_ENGINE = 'redis_sessions.session'
# redis 的配置文件
SESSION_REDIS_HOST = '127.0.0.1'
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = ''
SESSION_REDIS_PREFIX = 'session'  # 会话的开始标识

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注