1)模型的典型用法。
from django.db import models
class Author(models.Model):
STATUS_CHOICES = (
(1,"Needs Edit"),
(2,"Needs Aprroval"),
(3,"Published"),
(4,"Archived"),
)
first_name = models.CharField(max_length=30) #最大长度为30
last_name = models.CharField(max_length=40)
email = models.EmailField(blank=True) #允许为空(字符串型)
publication_date = models.DateField(blank=True,null=True ) #允许为空(非字符串型)
email = models.EmailField(verbose_name='e-mail') #自定义模板相应字段标签名为e-mail
status = models.IntegerField(choices=STATUS_CHOICES,default=1)
html_content = models.TextField(editable=False)
2)管理后台,应用注册典型用法。
from django.contrib import admin
from mysite.books.models import Publisher, Author, Book
class AuthorAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'email')
search_fields = ('first_name', 'last_name')
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'publisher',
'publication_date')
list_filter = ('publication_date',)
date_hierarchy = 'publication_date'
ordering = ('-publication_date',)
filter_horizontal = ('authors',)
raw_id_fields = ('publisher',)
fields = ('title', 'authors', 'publisher')
admin.site.register(Publisher)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)
3)django的模板系统默认是开启字符转义的,不像rails要写成<%=h xxxx %>,django默认就会进行转义,如果不想进行转义,可以使用管道safe。
This will be escaped: {{ data }}
This will not be escaped: {{ data|safe }}