Django笔记-ProgramRules

来源:互联网 发布:mac文件分类 编辑:程序博客网 时间:2024/06/04 23:31

 本节介绍部分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):

<本节完>

原创粉丝点击