标签:布尔 sqlite数据库 目录结构 rate stc 密码 page sql 时区
创建:mkvirtualenv [虚拟环境名称]
删除:rmvirtualenv [虚拟环境名称]
进入:workon [虚拟环境名称]
退出:deactivate
所有的虚拟环境,都位于/home/.virtualenvs目录下
进入虚拟环境前的提示:
进入虚拟环境后的提示:
查看当前的所有虚拟环境:workon [两次tab键]
查看虚拟环境中已经安装的包
pip list
pip freeze
pip install django==1.8.2
import django
django.get_version()
django-admin startproject test1
manage.py:一个命令行工具,可以使你用多种方式对Django项目进行交互
内层的目录:项目的真正的Python包
_init _.py:一个空文件,它告诉Python这个目录应该被看做一个Python包
settings.py:项目的配置
urls.py:项目的URL声明
wsgi.py:项目与WSGI兼容的Web服务器入口
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
python manage.py makemigrations
python manage.py migrate
python manage.py shell
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)
b
b.id
b.btitle
b.btitle=u"天龙八部" b.save()
b.delete()
h=HeroInfo() h.htitle=u‘郭靖‘ h.hgender=True h.hcontent=u‘降龙十八掌‘ h.hBook=b h.save()
b.heroinfo_set.all()
h=b.heroinfo_set.create(htitle=u‘黄蓉‘,hgender=False,hcontent=u‘打狗棍法‘)
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})
标签:布尔 sqlite数据库 目录结构 rate stc 密码 page sql 时区
原文地址:https://www.cnblogs.com/fwl8888/p/9345573.html