web 开发的历史
-
以前web开发,升级的时候需要
手动修改 html
-
CGI
NCSA(NCSA Mosaic 浏览器) –> 使得web server端 可以自动生成 html
Common Gateway Interface, or CGI 通用网关协议 CGI的发展推动了 动态网站的发展 缺点: CGI脚本需要包含很多的 重复的模板
-
PHP
的简易设计可以很好的嵌入 HTML缺点: 代码重复, 简单, 冗余, 漏洞 网络安全
-
Web-development frameworks
Django , Rails
关于作者
Malcolm Tredinnick 是 Django 的核心developer (R.I.P)
Two Scoops of Django 的 审阅者
coding style 代码风格
保持良好的代码习惯
,利于阅读和维护
➤ Avoid abbreviating variable names.
命名变量避免使用缩写
➤ Write out your function argument names.
写全函数的参数名
➤ Document your classes and methods.
注释类和方法
➤ Comment your code.
注释代码
➤ Refactor repeated lines of code into reusable functions or methods.
用函数和方法代替 重复的代码
➤ Keep functions and methods short. A good rule of thumb is that scrolling should not be necessary to read an entire function or method.
保持每个函数和方法精简, (不要用鼠标拖动 :) )
balance_sheet_decrease
比 缩写的bsd
or bal_s_d
要更容易理解, 记忆
PEP 8
PEP 8
is the official style guide for Python。python.org/dev/peps/pep-0008/
一些
PEP 8
的建议 :
➤ “Use 4 spaces per indentation level.”
缩进使用4个空格
➤ “Separate top-level function and class definitions with two blank lines.”
函数或者类之间间隔要用两个空行
➤ “Method definitions inside a class are separated by a single blank line.”
类方法之间的间隔使用一个空行
可以使用一些IDE auto-pep8插件工具 Flake8
,自动调节
保持已有项目的风格
当然如果已有的项目中代码风格不是`PEP8`, 需要遵从已有项目的风格
The 79-Character Limit
➤ On open source projects, there should be a hard 79-character limit. Our experience has shown
that contributors or visitors to these projects will grumble about line length issues; however, it
has not kept contributors away and we feel the value isn’t lost.
开源项目使用 79 limit
➤ On private projects, we relax the limit to 99 characters, taking full advantage of modern monitors.
个人项目使用 99 limit
import
顺序
1 Standard library imports
2 Related third-party imports
3 Local application or library specific imports
# Stdlib imports
from math import sqrt
from os.path import abspath
# Core Django imports
from django.db import models
from django.utils.translation import ugettext_lazy as _
# Third-party app imports
from django_extensions.db.models import TimeStampedModel
# Imports from your apps
from splits.models import BananaSplit
使用相对引用
# cones/views.py
from django.views.generic import CreateView
# Relative imports of the 'cones' package
from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin
class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm
Absolute
vs.Explicit Relative
vs.Implicit Relative
Code | Import Type | Usage |
---|---|---|
from core.views import FoodMixin | absolute import | Use when importing from outside thecurrent app |
from .models import WaffleCone | explicit relative | Use when importing from another module in the current app |
from models import WaffleCone | implicit relative | Often used when importing from another module in the current app, but not a good idea |
避免使用
import *
避免命名冲突
# 相同的变量名
from django.db.models import CharField
from django.forms import CharField
# rename
from django.db.models import CharField as ModelCharField
from django.forms import CharField as FormCharField