博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django笔记-ProgramRules
阅读量:4054 次
发布时间:2019-05-25

本文共 2783 字,大约阅读时间需要 9 分钟。

本节介绍部分django的编码规范:

settings.py中要使用相对路径:

import os
DIRNAME = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(DIRNAME, 'static')

example.com/
   README
   settings.py
   urls.py
   APP1/
   APP2/
   docs/
       This will hold the documentation for your project
   static/
       -In production this will be the root of your MEDIA_URL
       css/
       js/
       images/
   tests/
       - Project level tests (Each app should also have tests)
   uploads/
       - content imgs, etc
   templates/
       - This area is used to override templates from your reusable apps
       flatpages/
       comments/
       example/
       app1/
       app2/
      
Admin
非必须
放在 APP/admin.py 文件中
Admin 的 MODEL 类命名为 MODELAdmin
上下文处理器
放在 APP/context_processors.py 文件中
内容源
放在 APP/feeds.py 文件中
表单
放在 APP/forms.py 文件中
Managers
放在 APP/managers.py 文件中
中间件
放在 APP/middleware.py 文件中
实现尽可能少的任务
模型
放在 APP/models (.py 文件中或目录下)
遵循 Django’s 模型约定
模板
放在 APP/templates/APP/template.html 文件中

为了尽量标准化 Django 模板区块 (block) 名称, 我建议通常情况下使用以下区块名称.

{% block title %}
这个区块用来定义页面的标题. 你的 base.html 模板很可能要在这个 tag 之外定义站点名字 (Site’s name) (即便使用了 Sites 框架), 以便能够放在所有页面中.
{% block extra_head %}
我认为这是个非常有用的区块, 很多人已经以某种方式在使用了. 很多页面经常需要在 HTML 文档头添加些信息, 比如 RSS 源, Javascript, CSS, 以及别的应该放在文档头的信息. 你可以, 也很可能将会, 定义另外专门的区块 (比如前面的 title 区块) 来添加文档头的其它部分的信息.

{% block body %}

这个 tag 用来包含页面的整个 body 部分. 这使得你在 app 中创建的页面能够替换整个页面内容, 不仅仅是正文内容. 这种做法虽不常见, 但当你需要时, 它确实是一个非常方便的 tag. 你可能还没注意到, 我一直尽可能的使 tag 名字和 HTML 标签名称保持一致.

{% block menu %}

你的菜单 (导航栏) 应该包含在这个区块中. 它是针对站点级的导航, 不是每个页面专属的导航菜单.

{% block content %}

这个区块用来放置页面正文内容. 任何页面正文内容都可能不一样. 它不包含任何站点导航, 信息头, 页脚, 或其它任何属于 base 模板的东东.

其它可能的区块

{% block content_title %}
用来指定 content 区块的 “title”. 比如 blog 的标题. 也可以用来包含 content 内的导航 (译注: 比如提纲), 或其它类似的东东. 大致都是些页面中并非主要内容的东东. 我不知道这个区块是否应该放到 content tag 内, 并且对应于前面建议的 content tag, 是不是还需要一个 main_content 区块.

{% block header %} {% block footer %}

任何每个页面都可能修改的文本区域的页面和页脚.

{% block body_id %} {% block body_class %}

用来设置 HTML 文档 body 标签的 class 或 id 属性. 在设置样式或其它属性时非常有用.

{% block [section]_menu %} {% block page_menu %}

这是对应于之前建议的 menu 区块. 用来导航一个章节或页面.

模板标签

放在 APP/templatetags/APP_tags.py 文件中
推荐的模板标签语法
as (Context Var): This is used to set a variable in the context of the page
for (object or app.model): This is used to designate an object for an action to be taken on.
limit (num): This is used to limit a result to a certain number of results.
exclude (object or pk): The same as for, but is used to exclude things of that type.
测试
放在 APP/tests (.py 文件或目录) 中
Fixtures 放在 APP/fixtures/fixture.json 文件中
通常只须重写 Django 的 testcase
URLs
放在 APP/urls (.py 文件或目录) 中
需要设置 name 属性以便能够被反查; name 属性设置成 APP_MODEL_VIEW 的格式, 比如 blog_post_detail 或 blog_post_list.
视图
放在 APP/views (.py 文件或目录) 中
可以是任何可调用的 python 函数.
视图参数应提供合理的缺省值, 并易于定制:
范例:
def register(request, success_url=None,
         form_class=RegistrationForm
         template_name='registration/registration_form.html',
         extra_context=None):

<本节完>

 

原文网址:

转载地址:http://dzxci.baihongyu.com/

你可能感兴趣的文章
JavaScript的一些基础-数据类型
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
Visual Studio 2010:C++0x新特性
查看>>