python django tutorial學習筆記

www.youtube.com/
1. django_test chenxu$ python manage.py sql article
生成sql語句

django_test chenxu$ python manage.py syncdb
數據庫的生成

python manage.py reset article;
作用:remove table from database;

使用python manage.py shell
交互模式
from article.models import Artile
Article.objects.all()
a = Article(title='test1',body='body1',pub_date=timezone.now)
def __unicode__(self):
return self.title
幫助輸出object對象內容,而不是一個指針地址;

2. python django tutorial 3
views,simple urls and basic templates

編寫views
def hello(request):
return HttpResponse('hello')
修改 project/urls.py
url(r'^hello/$', 'article.views.hello', name='hello'),

區別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.

3. python django tutorial 4
advanced views and urls
app中增加urls.py, 只和article app相關
project/urls.py: url(r'^articles/', include('article.urls')),
articles/urls.py: url(r'^all/$','views.articles'),
url(r'^get/(?P<article_id>\d+)/$','views.article'),
創建articles/templates/articles.html
/article.html
sqlite3命令
sqlite3 storage.db
.help獲得幫助
.tables 顯示當前表格
select * from tablename;

4. python django tutorial 5
the built in Admin interface
打開installed_app: admin
碰到問題,無法登陸 localhost:8000/admin, 因爲賬號密碼錯誤
解決辦法:重新創建superuser
python manage.py createsuperuser --username=chenx [email protected]

5. python django tutorial 6
django template language, 模版語言
app/templates 從原來project目錄遷移到app下面,保證可以複用;
模版繼承,提高效率
{{article.body|lower|truncatewords:"20"}}

truncatewords: 摘要模式顯示(否則顯示全部內容)
lower: 小寫設置

{% if articles.count > 0%}
{%else %}
None to show

<%endif%

base.html
ariticles.html繼承,並且可以重寫其中的block content, block sidebar
繼承template語法:
{% extends "base.html"%}

繼承重寫內容
{%block content%}
{%endblock%}

繼承重寫邊框
{%block sidebar%}
{%endblock%}

6. python django tutorial
static file, 靜態文件
抽出css文件,放在static文件夾中
在project_test下面,增加文件夾static/css, static/images

http://localhost:8000/static/css/default.css
查看default.css 報錯
解決辦法:
需要註冊到settings.py
STATICFILES_DIRS = (
('assets','/Users/chenxu/work/python/django_test/static'),
)


修改base.html
<link rel="stylesheet" type="text/css" href="{%static "assets/css/default.css"%}">
{%load static%}

命令:
python manage.py collectstatic
注意路徑正確,另外配置是tuple

瀏覽器訪問地址
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

參考cookie, session其他的api
https://docs.djangoproject.com/en/dev/topics/http/sessions/

8. python django tutorial 9
user login and logout
installed_app: contrib.auth
middleware: authenticationMiddleware

訪問http://localhost:8000/accounts/login/
問題:
TemplateDoesNotExist at /accounts/login/
解決辦法:
settings.py
TEMPLATE_DIRS = (
('/Users/chenxu/work/python/django_test/templates'),
('/Users/chenxu/work/python/django_test/articles/templates'),
)此處要tuple數據格式

9. python django tutorial 10
user registration basics
學習如何增加註冊功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章