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
学习如何增加注册功能