Python之六-Django

Python之六-Django

 Django approach

--URLs decoupled from code

  high-level python web framework

MTV

 

installation

  Python 2.5+

  DB:PostgreSQL,MySQL,SQLite3

  Python DB Interface:psycopg,MySQLdb,pysqlite

  Django >1.0

 

 

sample

 放到GAE上面去跑

 

Django installation

  pip install Django==1.6.1

 

sample mysite

http://www.informit.com/articles/article.aspx?p=1273658

step1:create the project:mysite 

D:\test\mysite>python %django_home%\bin\django-admin.py startproject mysite

修改settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
       
    }
}

 INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
)

#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-cn'

#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

 

step2:running the development server

D:\test\mysite\mysite>python manage.py runserver
Validating models...

0 errors found
February 03, 2014 - 23:58:25
Django version 1.6.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[03/Feb/2014 23:58:36] "GET / HTTP/1.1" 200 1757

 

step3: create the  blog application

./manage.py startapp blog # or ".\manage.py startapp blog"

产生一个blog目录。

在settings.py里面修改配置

To tell Django this new app is part of your project, you need to edit settings.py (which we can also refer to as your “settings file”). Open your settings file in your editor and find theINSTALLED_APPS tuple near the bottom. Add your app in dotted module form to that tuple in a line that looks like this (note the trailing comma):

'mysite.blog',注意我们这个版本应该使用'blog'

 

http://jingyan.baidu.com/article/ed15cb1b0cd6081be36981b0.html

修改urls.py

urlpatterns = patterns('',
    # Examples:
    #url(r'^$', 'mysite.views.home', name='home'),
    url(r'^blog/', include('blog.urls')),
    url(r'^blog/index/$', 'blog.views.index'),
    url(r'^admin/', include(admin.site.urls)),
)

step4:

Designing Your Model

We’ve now arrived at the core of your Django-based blog application: the models.py file. This is where we’ll define the data structures of the blog. Following the principle of Don’t Repeat Yourself (DRY), Django gets a lot of mileage out of the model information you provide for your application. Let’s create a basic model, and then see all the stuff Django does for us using that information.

Open up models.py in your favorite text editor (bonus points if it has a Python mode with syntax coloring). You see this placekeeper text:

from django.db import models

# Create your models here.

Delete the comment, and then add the following lines:

class BlogPost(models.Model):
    title = models.CharField(max_length=150)
    body = models.TextField()
    timestamp = models.DateTimeField()

 

D:\test\mysite\mysite>python .\manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table blog_blogpost

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'jian.zhang'):
Email address: cajan2@163.com
Password:
Password (again):
Error: Your passwords didn't match.
Password:
Password (again):
Error: Your passwords didn't match.
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

修改urls.py

urlpatterns = patterns('',
    # Examples:
    #url(r'^$', 'mysite.views.home', name='home'),
    #url(r'^blog/', include('blog.urls')),
    url(r'^blog/index/$', 'blog.views.index'),
    url(r'^admin/', include(admin.site.urls)),
)

#from django.shortcuts import render

修改blog\views.py
from django.http import HttpResponse
# Create your views here.
def index(req):
  return HttpResponse('<h1>hello world, welcome to Django</h1>')
 

D:\test\mysite\mysite>python .\manage.py runserver
Validating models...

0 errors found
February 04, 2014 - 12:06:16
Django version 1.6.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[04/Feb/2014 12:06:47] "GET / HTTP/1.1" 404 2007
[04/Feb/2014 12:07:04] "GET /blog/index/ HTTP/1.1" 200 39

 

http://localhost:8000/blog/index/

 

总结:

  1.django install

  2.django-admin startproject mysite

  3.django-admin startapp blog

  4.修改settings.py

    app add---->blog

  5.urls.py

       url(r'^blog/index/$', 'blog.views.index'),
  6.blog/views.py

from django.http import HttpResponse
# Create your views here.
def index(req):
  return HttpResponse('<h1>hello world, welcome to Django</h1>'

 7.python manager.py runserver

  url:  localhost:8000

localhost:8000\blog\index/

/

几个不错的学习资源

withdjango.com

djangobook.com

百度经验-Django教程

 http://jingyan.baidu.com/season/36186


官方文档-入门介绍

https://docs.djangoproject.com/en/1.6/intro/

官方文档

https://docs.djangoproject.com/en/1.6/

认识django1.6(1)---官方环境搭建及poll应用练习过程总结

http://blog.csdn.net/julius_lee/article/details/16115955

4款基于Django框架的开源软件推荐

http://www.csdn.net/article/2011-05-16/297861

Python系列视频教程: Django【16讲】

http://down.51cto.com/zt/4091/1


Django book 2.0 的中文翻译。

http://djangobook.py3k.cn/2.0/

Python Web 框架,第 1 部分: 使用 Django 和 Python 开发 Web 站点

http://www.ibm.com/developerworks/cn/linux/l-django/index.html#icomments

中谷 Python Django 教程[13讲]

http://www.tudou.com/plcover/27e6koAtzTo/

Django Best Practices -Django开发的最佳实践

http://blog.objcc.com/django-best-practices/

 

django中国社区

http://www.djangochina.cn/

http://www.djangochina.cn/forum.php?mod=viewthread&tid=203&extra=page%3D1

 

Python高效编程技巧

Python yield 使用浅析

我们可以得出以下结论:

一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(在 for 循环中会自动调用 next())才开始执行。虽然执行流程仍按函数的流程执行,但每执行到一个 yield 语句就会中断,并返回一个迭代值,下次执行时从 yield 的下一个语句继续执行。看起来就好像一个函数在正常执行的过程中被 yield 中断了数次,每次中断都会通过 yield 返回当前的迭代值。

yield 的好处是显而易见的,把一个函数改写为一个 generator 就获得了迭代能力,比起用类的实例保存状态来计算下一个 next() 的值,不仅代码简洁,而且执行流程异常清晰。

如何判断一个函数是否是一个特殊的 generator 函数?可以利用 isgeneratorfunction 判断:

return 的作用

在一个 generator function 中,如果没有 return,则默认执行至函数完毕,如果在执行过程中 return,则直接抛出 StopIteration 终止迭代。

 

Python打印格式化与字符串

 

utf8的python源代码文件,好像windows下面的python不支持啊 

python utf8的乱码问题

 

 python utf-8 乱码

Python学习4:生成器、lamda表达式及map、reduce、filter函数

Python的lambda匿名函数

python的函数式特性

Python string的一些用法

#substr.py

# 使用split()获取子串
sentence = "Bob said: 1, 2, 3, 4"
print "使用空格取子串:", sentence.split()
print "使用逗号取子串:", sentence.split(",")
#maxsplit表示到第几个分隔符停止继续分割,也就是做了maxsplit次分割
print "使用maxsplit取子串:", sentence.split(",", 1)
print "使用maxsplit取子串:", sentence.split(",", 2)
print "使用maxsplit取子串:", sentence.split(",", 3)

# 字符串连接后将分配新的空间
str1 = "a"
print id(str1)
print id(str1 + "b")  #我们建议使用join进行多个字符串的连接

# 特殊切片截取子串
str1 = "hello world"
print word[0:3]
print str1[::2]     #::表示特殊的,2步长
print str1[1::2]
print str1[::4]     #::表示特殊的,4步长

DFA 文字过滤替换

用python实现文件比较

python参数中的*和**

python 获得命令行参数的方法

戏说WSGI(Python Web服务网关接口)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值