天津11选5开奖号结果查询:Djiango視圖-url???HttpRequest-HttpResponse-db_cache數據緩存

作者: 魯智深 分類: django 發布時間: 2018-01-14 23:28

天津11选5中奖结果 www.ebzvaz.com.cn 視圖接受Web請求并且返回Web響應

視圖就是一個python函數,被定義在views.py中

響應可以是一張網頁的HTML內容,一個重定向,

一個404錯誤等等 響應處理過程如下圖:

視圖

視圖

下面是一個簡單的路由選擇??椋║RLconf)的示例

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請求方法等等都將路由到相同的函數。
例如,//www.example.com/myapp/請求中,URLconf 將查找myapp/。
在//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'  # 會話的開始標識

如果覺得我的文章對您有用,請隨意打賞。您的支持將鼓勵我繼續創作!

發表評論

電子郵件地址不會被公開。 必填項已用*標注