随笔分类 - Django
摘要:新建一个项目 app02 在 app02/ 下创建 urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ url(r'^blog/', views.test, name="blog"), ]
阅读全文
posted @ 2019-08-01 22:14
Sch01aR#
摘要:通过 Django 建立的表 命名方式为:项目名_表名 可以将该默认命名方式进行修改 models.py: from django.db import models class Person(models.Model): id = models.AutoField(primary_key=True)
阅读全文
posted @ 2019-08-01 21:05
Sch01aR#
摘要:字段参数: null:用于表示某个字段可以为空 unique:如果设置为 unique=True,则该字段在此表中必须是唯一的 db_index:如果 db_index=True,则代表着为此字段设置数据库索引 default:为该字段设置默认值 关系字段参数: to:设置要关联的表 to_fiel
阅读全文
posted @ 2019-08-01 20:53
Sch01aR#
摘要:用 CharField 定义的字段在数据库中存放为 verchar 类型 自定义 char 类型字段需要下面的代码: class FixedCharField(models.Field): """ 自定义的 char 类型的字段类 """ def __init__(self, max_length,
阅读全文
posted @ 2019-08-01 20:13
Sch01aR#
摘要:BigAutoField(AutoField): bigint 自增列,必须填入参数 primary_key=True 如果没有写自增列,则会自动创建一个列名为 id 的列 SmallIntegerField(IntegerField): 短整型,-32768 到 32767 PositiveSma
阅读全文
posted @ 2019-08-01 19:39
Sch01aR#
摘要:AutoField: int 自增列,必须填入参数 primary_key=True 如果没有写 AutoField,则会自动创建一个列名为 id 的列 from django.db import models class Person(models.Model): id = models.Auto
阅读全文
posted @ 2019-08-01 18:38
Sch01aR#
摘要:命名 URL: test.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试页面</title> </head> <body> <p>测试页面</p> <form action="/test/"
阅读全文
posted @ 2019-08-01 15:45
Sch01aR#
摘要:URL 配置: urls.py 中的基本配置: from django.conf.urls import url from app01 import views urlpatterns = [ url(正则表达式, views 视图函数[, 参数, 别名]), ] 在 Django 2 版本后将 u
阅读全文
posted @ 2019-07-31 17:21
Sch01aR#
摘要:用 json 模块和 HttpResponse 返回生成的 json views.py: from django.shortcuts import render, HttpResponse import json # json 测试 def json_test(request): data = {"
阅读全文
posted @ 2019-07-30 20:30
Sch01aR#
摘要:request.method: 获取请求的方法,例如 GET、POST 等 views.py: from django.shortcuts import render, HttpResponse # request 对象 def test(request): print(request.method
阅读全文
posted @ 2019-07-30 18:27
Sch01aR#
摘要:FBV: Function Base View,基于函数的视图 views.py: from django.shortcuts import render, HttpResponse # FBV def upload(request): if request.method == "POST": fi
阅读全文
posted @ 2019-07-30 17:50
Sch01aR#
摘要:upload.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传页面</title> </head> <body> <form action="/upload/" method="post" e
阅读全文
posted @ 2019-07-30 17:32
Sch01aR#
摘要:simple_tag: simple_tag 和自定义 filter 类似,但可以接收更多更灵活的参数 在 app01/templatetags/ 目录下创建 mysimple_tag.py mysimple_tag.py: from django import template register
阅读全文
posted @ 2019-07-29 20:51
Sch01aR#
摘要:静态文件的路径设置在 settings.py 中 如果该路径发生更改的话,html 中相关路径也要进行修改 CSS: <link href="/static/dashboard.css" rel="stylesheet"> 可以写成: {% load static %} <link href="{%
阅读全文
posted @ 2019-07-29 19:27
Sch01aR#
摘要:网站中通常会有一个导航条,如下图 这个导航条在很多页面都会存在 可以把导航条做成一个组件,让要显示导航条的网页包含 导航条组件 nav.html: <h1>假装这是一个导航条</h1> 用 muban_test.html 来导入: <hr>{# 导入导航条组件 #} {% include 'nav.
阅读全文
posted @ 2019-07-29 18:05
Sch01aR#
摘要:可以把多个页面相同的部分提取出来,放在一个母板里,这些页面只需要继承这个母板就好了 通常会在母板中定义页面专用的 CSS 块和 JS 块,方便子页面替换 定义块: {% block 名字 %} {% endblock %} views.py 中添加函数: from django.shortcuts
阅读全文
posted @ 2019-07-29 17:21
Sch01aR#
摘要:标签使用 {% %} 注释语句:{# #} for 循环: views.py: from django.shortcuts import render, redirect, HttpResponse from app01 import models # Filter 测试 def filter_te
阅读全文
posted @ 2019-07-26 21:29
Sch01aR#
摘要:自定义过滤器的文件: 在 app01 下新建一个 templatetags 的文件夹,然后创建 myfilter.py 文件 这个 templatetags 名字是固定的,myfilter 是自己起的 myfilter.py: from django import template register
阅读全文
posted @ 2019-07-26 20:50
Sch01aR#
摘要:通过管道符 "|" 来使用过滤器,{{ value|过滤器:参数 }} Django 的模板语言中提供了六十个左右的内置过滤器 urls.py: from django.conf.urls import url from django.contrib import admin from app01
阅读全文
posted @ 2019-07-26 19:56
Sch01aR#
摘要:前言: 在 Django 模板语言中变量用 {{ }},逻辑用 {% %} 在 urls.py 中添加对应关系 from django.conf.urls import url from django.contrib import admin from app01 import views urlp
阅读全文
posted @ 2019-07-25 22:57
Sch01aR#

浙公网安备 33010602011771号