zoukankan      html  css  js  c++  java
  • django学习笔记(三)

    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 }}

  • 相关阅读:
    IDEA中Git实战
    高并发下redis缓存穿透问题解决方案
    springboot整合mybatis+generator
    Github修改项目显示的语言类型
    Github中README.md换行
    maven中可以直接引用的java系统属性和环境变量属性
    springboot启动异常java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
    java获取Linux持续运行时间及友好显示
    http工具类
    idHTTP最简洁的修改和取得Cookie例子
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426695.html
Copyright © 2011-2022 走看看