Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for Django views?

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)?

like image 218
RexE Avatar asked May 17 '09 20:05

RexE


3 Answers

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,
    })
like image 146
Soviut Avatar answered Nov 20 '22 06:11

Soviut


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.

like image 23
Neil Trodden Avatar answered Nov 20 '22 06:11

Neil Trodden


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.

like image 1
Macke Avatar answered Nov 20 '22 06:11

Macke