Rex*_*exE 5 django naming-conventions
我正在构建一个网站(在Django中)并且对于用于我的函数的正确命名约定感到困惑.简单的例子:假设我有一个页面让用户决定是否要查看图像A或图像B.一旦用户提交决定,该网站就会显示用户请求的图像.
以下是我在视图模块中可以使用的两个函数:
def function1(request):
"""Returns the page that presents the user with the choice between A and B"""
def function2(request):
"""Takes in the submitted form and returns a page with the image the user requested."""
Run Code Online (Sandbox Code Playgroud)
命名执行此操作的函数的约定是什么?我看到至少两种可行的方法:
选项1:function1: "decide", function2: "view_image"
选项2:function1: "view_choices", function2: "decide"
中心问题是这些功能中的每一个都做两件事:(1)处理和存储用户提交的数据,以及(2)返回下一页,其可能与用户的输入相关或不相关.那么我应该在(1)或(2)之后命名我的功能吗?
通常,约定是某种CRUD(创建,检索,更新,删除).我个人使用索引,详细信息,创建,更新,删除我的操作.但是,我不认为这适用于您的自定义功能.
真的,这听起来你的功能应该合并到相同的"选择"功能.然后,您可以显示表单或结果,具体取决于结果是否为POST.
注意:我在表单处理方面从django文档中大量补充了这个例子.
def choose(request):
"""
Presents the user with an image selection form and displays the result.
"""
if request.method == 'POST': # If the form has been submitted...
form = ChoiceForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ChoiceForm() # An unbound form
return render_to_response('choose.html', {
'form': form,
})
Run Code Online (Sandbox Code Playgroud)
只要您有这些好的评论,我怀疑这对您来说永远不会成为问题。
无论如何,最好根据功能来命名函数,因此 function1 可以是“displayImageChoices”,function2 可以是“displayImage”。
IE 中,function1 接受一些输入并显示一些选择,function2 接受一些输入并显示图像。
| 归档时间: |
|
| 查看次数: |
3356 次 |
| 最近记录: |