django临时
搜类几个链接
http://blog.csdn.net/yima1006/article/details/7028924
http://blog.sina.com.cn/s/blog_4a27780601012wnj.html
http://www.cnblogs.com/cacique/archive/2012/09/30/2709143.html
http://blog.csdn.net/kevin6216/article/details/6913005
http://djangobook.py3k.cn/2.0/chapter01/
cd /data/rootdir
django-admin.py startproject mysite
cd mysite && ./manage.py startapp blog
激活
vim mysite/settings.py
INSTALLED_APPS = (
'mysite.blog',
in:/data/rootdir/mysite # vim mysite/settings.py
jin:/data/rootdir/mysite # ./manage.py runserver
Error: No module named blog
修改为'blog',
设计model
vim blog/models.py
设置数据库
jin:/data/rootdir/mysite # vim mysite/settings.py
DATABASES = {
'default': {
'ENGINE': ‘django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangodb', # Or path to database file if using sqlite3.
'USER': 'jin', # Not used with sqlite3.
'PASSWORD': '12111', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
验证
jin:/data/rootdir/mysite # python manage.py validate
Error: No module named blog
运行 python manage.py shell 进入
輸入下面這些命令來測試你的數據庫配置:
>>> from django.db import connection
>>> cursor = connection.cursor()
Error: No module named blog
这里INSTALLED_APPS = (
'mysite.blog', 就不行
改为blog
./manage.py syncdb
、 设置自动admin应用
jin:/data/rootdir/mysite # vim mysite/settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
./manage.py syncdb
设置好有,需要指定一个url访问
jin:/data/rootdir/mysite # vim mysite/urls.py
from django.contrib import admin
admin.autodiscover()
url(r'^admin/', include(admin.site.urls)),
这里不是字符串 是变量
在设置admin的model
jin:/data/rootdir/mysite # vim blog/models.py
from django.db import models
from django.contrib import admin
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title', 'timestamp')
admin.site.register(BlogPost, BlogPostAdmin)
创建表
jin:/data/rootdir/mysite # ./manage.py syncdb
./manage.py runserver &
建立Blog的公共部分
从Django的角度来说,一个页面具有三个典型的组件:
* 一个模板(template),模板负责将传递进来的信息显示出来(用一种类似Python字典的对象Context)。
* 一个视图(view)函数,它负责获取要显示的信息,通常都是从数据库里取得。
* 一个URL模式,它用来把收到的请求和你的视图函数匹配,有时也会向视图传递一些参数。
1)创建模板
jin:/data/rootdir/mysite # vim blog/templates/archive.html
<!– filename = archive.html –>
{% extends “base.html” %}
{% block content %}
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.timestamp|date:”l, F jS” }}</p>
<p>{{ post.body }}</p>
{% endfor %}
{% endblock %}
jin:/data/rootdir/mysite # vim blog/templates/base.html
<!– filename = base.html –>
<html>
<style type=”text/css”>
body { color: #efd; background: #453; padding: 0 5em; margin: 0}
h1 { padding: 2em 1em; background: #675 }
h2 { color: #bf8; border-top: 1px dotted #fff; marin-top: 2em }
p { margin: lem 0 }
</style>
<body>
<h1>blog for django –designed by libear</h1>
{% block content %}
{% endblock %}
</body>
</html>
2)创建视图
jin:/data/rootdir/mysite # vim blog/views.py
# Create your views here.
from django.template import loader, Context
from django.http import HttpResponse
from mysite.blog.models import BlogPost
def archive(request):
posts = BlogPost.objects.all()
t = loader.get_template('archive.html')
c = Context({ 'posts': posts })
return HttpResponse(t.render(c))
每个Django视图函数都将django.http.HttpRequest对象作为它的第一个参数,每个Django视图函数都会返回一个django.http.HttpResponse对象。
3)创建一个URL模式
jin:/data/rootdir/mysite # vim mysite/urls.py
增加语句:
url(r'^blog/', include(mysite.blog.urls)),
在blog根目录下创建urls.py:
jin:/data/rootdir/mysite # vim blog/urls.py
# filename = urls.py
from django.conf.urls.defaults import *
from mysite.blog.views import archive
urlpatterns = patterns('',
url(r'^$', archive), # archive is ‘mysite.blog.views.archive’
)
访问报错
Exception Value:
name 'mysite' is not defined
jin:/data/rootdir/mysite # vim mysite/urls.py
url(r'^blog/', include(blog.urls)),
Exception Value: |
name 'blog' is not defined |
---|
urlpatterns = patterns('',
url(r'^blog/', include(mysite.blog.urls)),
)
vim mysite/settings.py修改
'mysite.blog',
挂类