Reputation: 17733
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)?
Upvotes: 6
Views: 5763
Reputation: 3466
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():
...
Upvotes: -1
Reputation: 91595
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,
})
Upvotes: 9
Reputation: 25690
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.
Upvotes: 2
Reputation: 4728
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.
Upvotes: 1