Django 视图的命名约定?

发布于 2024-07-20 21:39:27 字数 727 浏览 5 评论 0原文

我正在构建一个网站(在 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."""

命名执行此操作的函数的约定是什么? 我看到至少有两种可行的方法:

选项 1: function1: "decide", function2: "view_image"

选项 2: function1 : "view_choices", function2: "decide"

核心问题是这些函数中的每一个都做了两件事:(1) 处理和存储用户提交的数据,以及 (2) 返回下一页,这可能会导致数据丢失。或者可能与用户的输入无关。 那么我应该以 (1) 还是 (2) 命名我的函数呢?

I'm building a website (in Django) and am confused about the right naming convention to use for my functions. Trivial example: let's say I have a page that lets the user decide whether they want to see image A or image B. Once the user submits the decision, the site displays the image the user requested.

Here are the two functions I would have in my views module:

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."""

What is the convention for naming the functions that do this? I see at least two feasible ways:

Option 1: function1: "decide", function2: "view_image"

Option 2: function1: "view_choices", function2: "decide"

The central issue is that each of these functions does 2 things: (1) process and store the data the user submitted, and (2) return the next page, which may or may not be related to the user's input. So should I name my functions after (1) or (2)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如何视而不见 2024-07-27 21:39:27

通常,约定是某种 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,
    })

Typically the convention is some kind of CRUD (create, retrieve, update, delete). I personally use index, detail, create, update, delete for my actions. However, I don't think this applies to your custom functions.

Really it sounds like your functions should be merged into the same "choose" function. You then either display the form or the result depending on whether the result was a POST or not.

Note: I've heavily coppied this example form the django docs on form handling.

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,
    })
那请放手 2024-07-27 21:39:27

在适用的情况下,我会使用类似于内置视图(object_list、object_detail 等)的东西。 (总是一个好主意)

其余部分将尽可能遵循该概念(item_action)。

I'd use something similar to the built in views (object_list, object_detail, etc) where applicable. (Always a good idea)

The rest would then follow that notion (item_action) as far as possible.

茶色山野 2024-07-27 21:39:27

只要您有这些好的评论,我怀疑这对您来说永远不会成为问题。

无论如何,最好根据功能来命名函数,因此 function1 可以是“displayImageChoices”,function2 可以是“displayImage”。

IE 中,function1 接受一些输入并显示一些选择,function2 接受一些输入并显示图像。

As long as you have those nice comments in, I suspect it won't ever be an issue for you.

Anyway, it's best to name functions based on what they do so function1 could be "displayImageChoices" and function2 could be "displayImage".

IE, function1 takes some input and displays some choices, function2 takes some input and displays an image.

一梦浮鱼 2024-07-27 21:39:27

我知道这现在有点过时了,但是自从切换到基于类的视图以来。

PEP8 (python.org/dev/peps/pep-0008) 类的命名约定每个单词都要大写,没有空格。 如果您仍然使用函数样式视图,那么它将是小写函数名称,并用下划线替换空格以提高可读性。

例如,基于类的视图:

class MyClassBasedView():
    ...

基于函数

def my_function_based_view():
    ...

I know this is a little outdated now, but since the switch to class based views.

PEP8 (python.org/dev/peps/pep-0008) naming conventions for classes would be capitalisation of each word, with no spaces. If you still utilise the function style views, then it would be lower case function names with spaces replaced by underscores for readability.

For example with class based views:

class MyClassBasedView():
    ...

Function based

def my_function_based_view():
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文