标签:功能 server 密码 属性 取值 led 获取值 表达式 oct
http://www.itcast.cn/python/1/?i=1&p=new,只匹配“/python/1/”部分
url(r‘^([0-9]+)/$‘, views.detail, name=‘detail‘),
url(r‘^(?P<id>[0-9]+)/$‘, views.detail, name=‘detail‘),
from django.conf.urls import include, url urlpatterns = [ url(r‘^‘, include(‘booktest.urls‘, namespace=‘booktest‘)), ]
请求http://www.itcast.cn/booktest/1/ 在sesstings.py中的配置: url(r‘^booktest/‘, include(‘booktest.urls‘, namespace=‘booktest‘)), 在booktest应用urls.py中的配置 url(r‘^([0-9]+)/$‘, views.detail, name=‘detail‘), 匹配部分是:/booktest/1/ 匹配过程:在settings.py中与“booktest/”成功,再用“1/”与booktest应用的urls匹配
新建views1.py #coding:utf-8 from django.http import HttpResponse def index(request): return HttpResponse("你好") 在urls.py中修改配置 from . import views1 url(r‘^$‘, views1.index, name=‘index‘),
<!DOCTYPE html> <html> <head> <title></title> </head> <body> 找不到了 <hr/> {{request_path}} </body> </html>
DEBUG = False ALLOWED_HOSTS = [‘*‘, ]
http://127.0.0.1:8000/test/
Request对象
dict.get(‘键‘,default) 或简写为 dict[‘键‘]
dict.getlist(‘键‘,default)
def getTest1(request): return render(request,‘booktest/getTest1.html‘) def getTest2(request): return render(request,‘booktest/getTest2.html‘) def getTest3(request): return render(request,‘booktest/getTest3.html‘)
url(r‘^getTest1/$‘, views.getTest1), url(r‘^getTest2/$‘, views.getTest2), url(r‘^getTest3/$‘, views.getTest3),
<html> <head> <title>Title</title> </head> <body>
链接1:一个键传递一个值 <a href="/getTest2/?a=1&b=2">gettest2</a><br> 链接2:一个键传递多个值 <a href="/getTest3/?a=1&a=2&b=3">gettest3</a> </body> </html>
def getTest2(request): a=request.GET[‘a‘] b=request.GET[‘b‘] context={‘a‘:a,‘b‘:b} return render(request,‘booktest/getTest2.html‘,context)
<html> <head> <title>Title</title> </head> <body> a:{{ a }}<br> b:{{ b }} </body> </html>
def getTest3(request): a=request.GET.getlist(‘a‘) b=request.GET[‘b‘] context={‘a‘:a,‘b‘:b} return render(request,‘booktest/getTest3.html‘,context)
<html> <head> <title>Title</title> </head> <body> a:{% for item in a %} {{ item }} {% endfor %} <br> b:{{ b }} </body> </html>
def postTest1(request): return render(request,‘booktest/postTest1.html‘)
url(r‘^postTest1$‘,views.postTest1)
<html> <head> <title>Title</title> </head> <body> <form method="post" action="/postTest2/"> 姓名:<input type="text" name="uname"/><br> 密码:<input type="password" name="upwd"/><br> 性别:<input type="radio" name="ugender" value="1"/>男 <input type="radio" name="ugender" value="0"/>女<br> 爱好:<input type="checkbox" name="uhobby" value="胸口碎大石"/>胸口碎大石 <input type="checkbox" name="uhobby" value="跳楼"/>跳楼 <input type="checkbox" name="uhobby" value="喝酒"/>喝酒 <input type="checkbox" name="uhobby" value="爬山"/>爬山<br> <input type="submit" value="提交"/> </form> </body> </html>
def postTest2(request): uname=request.POST[‘uname‘] upwd=request.POST[‘upwd‘] ugender=request.POST[‘ugender‘] uhobby=request.POST.getlist(‘uhobby‘) context={‘uname‘:uname,‘upwd‘:upwd,‘ugender‘:ugender,‘uhobby‘:uhobby} return render(request,‘booktest/postTest2.html‘,context)
url(r‘^postTest2$‘,views.postTest2)
<html> <head> <title>Title</title> </head> <body> {{ uname }}<br> {{ upwd }}<br> {{ ugender }}<br> {{ uhobby }} </body> </html>
#coding=utf-8 from django.http import HttpResponse def index(request): return HttpResponse(‘你好‘) 调用模板 from django.http import HttpResponse from django.template import RequestContext, loader def index(request): t1 = loader.get_template(‘polls/index.html‘) context = RequestContext(request, {‘h1‘: ‘hello‘}) return HttpResponse(t1.render(context))
from django.http import HttpResponse from datetime import * def index(request): response = HttpResponse() if request.COOKIES.has_key(‘h1‘): response.write(‘<h1>‘ + request.COOKIES[‘h1‘] + ‘</h1>‘) response.set_cookie(‘h1‘, ‘你好‘, 120) # response.set_cookie(‘h1‘, ‘你好‘, None, datetime(2016, 10, 31)) return response
在views1.py中 from django.http import HttpResponse,HttpResponseRedirect def index(request): return HttpResponseRedirect(‘js/‘) def index2(request,id): return HttpResponse(id) 在应用的urls.py中增加一个url对象 url(r‘^([0-9]+)/$‘, views1.index2, name=‘index2‘),
from django.core.urlresolvers import reverse def index(request): return HttpResponseRedirect(reverse(‘booktest:index2‘, args=(1,)))
from django.http import JsonResponse def index2(requeset): return JsonResponse({‘list‘: ‘abc‘})
from django.shortcuts import render def index(request): return render(request, ‘booktest/index.html‘, {‘h1‘: ‘hello‘})
from django.shortcuts import redirect from django.core.urlresolvers import reverse def index(request): return redirect(reverse(‘booktest:index2‘))
from django.shortcuts import * def detail(request, id): try: book = get_object_or_404(BookInfo, pk=id) except BookInfo.MultipleObjectsReturned: book = None return render(request, ‘booktest/detail.html‘, {‘book‘: book}) 将settings.py中的DEBUG改为False 将请求地址输入2和100查看效果
from django.shortcuts import * def index(request): # list = get_list_or_404(BookInfo, pk__lt=1) list = get_list_or_404(BookInfo, pk__lt=6) return render(request, ‘booktest/index.html‘, {‘list‘: list}) 将settings.py中的DEBUG改为False
像INSTALLED_APPS列表中添加: ‘django.contrib.sessions‘, 向MIDDLEWARE_CLASSES列表中添加: ‘django.contrib.sessions.middleware.SessionMiddleware‘,
from django.shortcuts import render, redirect from django.core.urlresolvers import reverse def index(request): uname = request.session.get(‘uname‘) return render(request, ‘booktest/index.html‘, {‘uname‘: uname}) def login(request): return render(request, ‘booktest/login.html‘) def login_handle(request): request.session[‘uname‘] = request.POST[‘uname‘] return redirect(reverse(‘main:index‘)) def logout(request): # request.session[‘uname‘] = None # del request.session[‘uname‘] # request.session.clear() request.session.flush() return redirect(reverse(‘main:index‘))
主url: from django.conf.urls import include, url urlpatterns = [ url(r‘^‘, include(‘booktest.urls‘, namespace=‘main‘)) ] 应用url: from django.conf.urls import url from . import views urlpatterns = [ url(r‘^$‘, views.index, name=‘index‘), url(r‘login/$‘, views.login, name=‘login‘), url(r‘login_handle/$‘, views.login_handle, name=‘login_handle‘), url(r‘logout/$‘, views.logout, name=‘logout‘) ]
<!DOCTYPE html> <html> <head> <title>首页</title> </head> <body> 你好:{{uname}} <hr/> <a href="{%url ‘main:login‘%}">登录</a> <hr/> <a href="{%url ‘main:logout‘%}">退出</a> </body> </html>
<!DOCTYPE html> <html> <head> <title>登录</title> </head> <body> <form method="post" action="/login_handle/"> <input type="text" name="uname"/> <input type="submit" value="登录"/> </form> </body> </html>
def login_handle(request): request.session[‘uname‘] = request.POST[‘uname‘] # request.session.set_expiry(10) # request.session.set_expiry(timedelta(days=5)) # request.session.set_expiry(0) # request.session.set_expiry(None) return redirect(reverse(‘main:index‘))
SESSION_ENGINE=‘django.contrib.sessions.backends.db‘
SESSION_ENGINE=‘django.contrib.sessions.backends.cache‘
SESSION_ENGINE=‘django.contrib.sessions.backends.cached_db‘
pip install django-redis-sessions
SESSION_ENGINE = ‘redis_sessions.session‘ SESSION_REDIS_HOST = ‘localhost‘ SESSION_REDIS_PORT = 6379 SESSION_REDIS_DB = 0 SESSION_REDIS_PASSWORD = ‘‘ SESSION_REDIS_PREFIX = ‘session‘
启动:sudo redis-server /etc/redis/redis.conf 停止:sudo redis-server stop 重启:sudo redis-server restart redis-cli:使用客户端连接服务器 keys *:查看所有的键 get name:获取指定键的值 del name:删除指定名称的键
标签:功能 server 密码 属性 取值 led 获取值 表达式 oct
原文地址:http://www.cnblogs.com/alwaysInMe/p/7439820.html