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

  • 相关阅读:
    Linux下校验下载文件的完整性(MD5,SHA1,PGP)
    十六进制字节 & 十六进制转二进制
    session & cookie
    php与mysql的链接到底用mysql 还是mysqli,pdo
    php连接数据库时候的字符集设置
    Prepared statements(mysqli & pdo)
    shell脚本中执行mysql命令
    centos系统字符编码问题
    实用crontab命令
    shell获取用户输入
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426696.html
Copyright © 2011-2022 走看看