环境:win10x64
已经安装python2.7,django1.8 已经配置环境变量等
django-admin.exe startproject test5
cd test5
创建应用 python manage.py startapp blog
进入mysite目录修改settings.py文件,加入应用
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', )
设计模型models.py
from django.db import models # Create your models here. class BlogPost(models.Model): title = models.CharField(max_langth=150) body = models.TextField() timestamp = models.DateTimeField()
设置数据库:settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test5', 'USER': 'root', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '3306', } }
同步数据库:
python manage.py makemigrations blog python manage.py migrate
创建管理员:
python manage.py createsuperuser
修改models.py
from django.db import models from django.contrib import admin ... admin.site.register(BlogPost)
运行看到如图OK
进入blog 的add添加点东西吧
然后修改admin.py:
from django.contrib import admin from .models import * class BlogPostAdmin(admin.ModelAdmin): list_display = ('title','timestamp') admin.site.register(BlogPost,BlogPostAdmin)
这样在看里面的内容就可以显示出字段内容来了
添加html文件:应用下templates\archive.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <body> {%for post in posts%} <h2>`post`.`title`</h2> <p>`post`.`timestamp`</p> <p>`post`.`body`</p> {%endfor%} </body> </html>
修改视图:views.py
from django.shortcuts import render from django.http import HttpResponse from django.template import loader,Context from .models import * # Create your views here. def archive(req): posts = BlogPost.objects.all() t = loader.get_template('archive.html') c= Context({'posts':posts}) return HttpResponse(t.render(c))
修改urls.py
from django.conf.urls import include, url from django.contrib import admin from blog.views import * urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^$',archive), ]
模板:base.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>index</title> <style> body{color: #efd;background: #453;padding: - 5em;margin: 0} h1{padding: 2em 1em;background:#675;} h2{color: #bf8;} p{padding: 1em 0;} </style> </head> <body> <h1>mysite.xxx.com</h1> {%block content%} {%endblock%} </body> </html>
archive.html
{% extends 'base.html' %} {%block content%} {%for post in posts%} <h2>`post`.`title`</h2> <p>{{post.timestamp|date:"Y年m月d日"}}</p> <p>`post`.`body`</p> {%endfor%} {%endblock%}
丰富变量模型
修改models.py
#coding=utf-8 from django.db import models from django.contrib import admin class BlogPost(models.Model): title = models.CharField(max_length=150) body = models.TextField() timestamp = models.DateTimeField() class Meta: ordering = ('-timestamp',) class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) author = models.ManyToManyField(Author) class Meta: abstract = True #指明book是一个抽象类 def __unicode__(self): return self.title class SmithBook(models.Model): author = models.ManyToManyField(Author,limit_choices_to={'name_endswith':'Shith'}) class Person(models.Model): first = models.CharField(max_length=100) last = models.CharField(max_length=100) middle = models.CharField(max_length=100,blank=True) class Meta: ordering = ['last','first','middle'] unique_together = ['last','first','middle'] verbose_name_plural = 'people'
.。。以后继续更新