区别project and app
project: django_test
app: Article
使用templates
4种方式使用template
a. t = get_template('hello.html')
html = t.render(Context({'name':name}))
return HttpResponse(html)
该种方式知道使用tempalte的原理
b. render_to_response('hello.html',{'name':name})
最简单的方式,但是不明白其中的原理;
c.
d.
浏览器访问地址
http://localhost:8000/static/css/default.css
出现错误:
'css/default.css' could not be found
解决办法:
正确路径 http://localhost:8000/static/assets/css/default.css
7. python django tutorial 8
cookies and sessions
启用session模块
settings.py
django.contrib.session.middleware.SessionMiddleware
改写views.py,演示cookie的使用
错误:
Could not parse the remainder: '%language%' from '%language%'
解决办法:
titles.html
{%block content%}
<h2> language is {{language}}<h2>
<h2> session language is {{session_language}}</h2>
{%endblock%}
必须放在block内才会显示内容;
response = HttpResponse('setting language to %s' %language)
response.set_cookie('lang',language)
cookie,session api
if 'lang' in request.COOKIES:
language = request.COOKIES['lang']
if 'lang' in request.session:
session_language = request.session['lang']
response = HttpResponse('setting language to %s' %language)
response.set_cookie('lang',language)
request.session['lang'] = language