标签:启动 上下 reg .sh center body 完成 阶段 稳定性
这里简单的演示完成一个 django 大概流程,后面会有详细解释
以下使用的是 ubuntu 系统
在生产环境中,有的时候,我们开发的项目可能用的是几个不同的 环境,所以通常我们会创建虚拟环境,在虚拟环境中安装我们需要的 配置,这里从配置虚拟环境开始
pip list pip freeze
pip install django==1.8.2
import django django.get_version()
表格需求:略
python manage.py startapp booktest
from django.db import models class BookInfo(models.Model): btitle = models.CharField(max_length=20) bpub_date = models.DateTimeField() def _ _str_ _(self): return "%d" % self.pk class HeroInfo(models.Model): hname = models.CharField(max_length=20) hgender = models.BooleanField() hcontent = models.CharField(max_length=100) hBook = models.ForeignKey(‘BookInfo‘)
# 上面创建了管理,外键 默认为对方的 主键 def _ _str_ _(self): return "%d" % self.pk # 这里创建了两个类,分别继承了 django.db 中的 models.Model 类,表示需要生成表头 # 定义 str 方法,表示默认输出
python manage.py makemigrations # 表的字段(表头)无论是增加删除还是类型改变后,都应该重新迁移
python manage.py migrate
from booktest.models import BookInfo,HeroInfo from django.utils import timezone from datetime import *
BookInfo.objects.all()
b = BookInfo() b.btitle="射雕英雄传" b.bpub_date=datetime(year=1990,month=1,day=10) b.save()
# 其它方式后面介绍
b=BookInfo.objects.get(pk=1) # pk 代表的是 主键 的意思,这里代指 id
b.btitle=u"天龙八部" b.save()
b.delete()
h=HeroInfo() h.htitle=u‘郭靖‘ h.hgender=True h.hcontent=u‘降龙十八掌‘ h.hBook=b h.save()
python manage.py runserver ip:port
python manage.py runserver 8080
python manage.py createsuperuser,按提示输入用户名、邮箱、密码
LANGUAGE_CODE = ‘zh-Hans‘ TIME_ZONE = ‘Asia/Shanghai‘
from django.contrib import admin from models import BookInfo admin.site.register(BookInfo)
class QuestionAdmin(admin.ModelAdmin): ... admin.site.register(Question, QuestionAdmin)
list_display = [‘pk‘, ‘btitle‘, ‘bpub_date‘]
list_filter = [‘btitle‘]
search_fields = [‘btitle‘]
list_per_page = 10
fields = [‘bpub_date‘, ‘btitle‘]
fieldsets = [ (‘basic‘,{‘fields‘: [‘btitle‘]}), (‘more‘, {‘fields‘: [‘bpub_date‘]}), ]
对于HeroInfo模型类,有两种注册方式
按照BookInfor的注册方式完成HeroInfo的注册
from django.contrib import admin from models import BookInfo,HeroInfo class HeroInfoInline(admin.StackedInline): model = HeroInfo extra = 2 class BookInfoAdmin(admin.ModelAdmin): inlines = [HeroInfoInline] admin.site.register(BookInfo, BookInfoAdmin)
class HeroInfoInline(admin.TabularInline)
def gender(self): if self.hgender: return ‘男‘ else: return ‘女‘ gender.short_description = ‘性别‘
class HeroInfoAdmin(admin.ModelAdmin): list_display = [‘id‘, ‘hname‘, ‘gender‘, ‘hcontent‘]
#coding:utf-8 from django.http import HttpResponse def index(request): return HttpResponse("index") def detail(request,id): return HttpResponse("detail %s" % id)
url(r‘^‘, include(‘booktest.urls‘)),
from django.conf.urls import url from . import views urlpatterns = [ url(r‘^$‘, views.index), url(r‘^([0-9]+)/$‘, views.detail), ]
‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)],
{{输出值,可以是变量,也可以是对象.属性}} {%执行代码段%}
<!DOCTYPE html> <html> <head> <title>首页</title> </head> <body> <h1>图书列表</h1> <ul> {%for book in booklist%} <li> <a href="{{book.id}}"> {{book.btitle}} </a> </li> {%endfor%} </ul> </body> </html>
<!DOCTYPE html> <html> <head> <title>详细页</title> </head> <body> <h1>{{book.btitle}}</h1> <ul> {%for hero in book.heroinfo_set.all%} <li>{{hero.hname}}---{{hero.hcontent}}</li> {%endfor%} </ul> </body> </html>
from django.http import HttpResponse from django.template import RequestContext, loader from models import BookInfo def index(request): booklist = BookInfo.objects.all() template = loader.get_template(‘booktest/index.html‘) context = RequestContext(request, {‘booklist‘: booklist}) return HttpResponse(template.render(context)) def detail(reqeust, id): book = BookInfo.objects.get(pk=id) template = loader.get_template(‘booktest/detail.html‘) context = RequestContext(reqeust, {‘book‘: book}) return HttpResponse(template.render(context))
<a href="{{book.id}}">
url(r‘^book/([0-9]+)/$‘, views.detail),
url(r‘^admin/‘, include(admin.site.urls, namespace=‘booktest‘)),
url(r‘^book/([0-9]+)/$‘, views.detail, name="detail"),
<a href="{%url ‘booktest:detail‘ book.id%}">
from django.shortcuts import render from models import BookInfo def index(reqeust): booklist = BookInfo.objects.all() return render(reqeust, ‘booktest/index.html‘, {‘booklist‘: booklist}) def detail(reqeust, id): book = BookInfo.objects.get(pk=id) return render(reqeust, ‘booktest/detail.html‘, {‘book‘: book})
标签:启动 上下 reg .sh center body 完成 阶段 稳定性
原文地址:http://www.cnblogs.com/alwaysInMe/p/7425192.html