I was creating a view with Django that would create a user profile for a website and this is what I came up with:
if (request.POST):
required = set( ['username', 'email', 'first_name', 'last_name', 'password',
'address_1', 'city', 'state', 'zip_code'] )
if (not (required <= set(request.POST))):
return render( request, 'profiles/create.html', { 'missing' : required - set(request.POST) } )
username = request.POST['username']
email = request.POST['email']
if (not Profile.is_unique(email)):
return render( request, 'profiles/create.html', {'message' : 'Email address already exists!'} )
first_name = request.POST['first_name']
last_name = request.POST['last_name']
password = request.POST['password']
address_1 = request.POST['address_1']
address_2 = request.POST['address_2'] if 'address_2' in request.POST else None
city = request.POST['city']
state = request.POST['state']
zip_code = request.POST['zip_code']
new_id = Create(username, email, first_name, last_name, password, address_1,
city, state, zip_code, address_2)
return redirect('profiles.views.Details', profile_id = new_id)
else:
return render(request, 'profiles/create.html')
I am about 65% happy with this code as it is, especially the use of sets. However, there are some areas I would like to improve on:
- Is there any way I could avoid assigning to individual variables from the
request.POST
calls, maybe usingsetattr
? Would that be faster or slower than doing it this way? - I have a large
if
at the top which checks whether or not the page has been submitted already to avoid error messages popping up the first time somebody navigates to the page but I'm not sure if this is the best way to go about it.
1 Answer 1
Instead of doing:
required = set( ['username', 'email', 'first_name', 'last_name', 'password',
'address_1', 'city', 'state', 'zip_code'] )
You can just use {}
:
required = {'username', 'email', 'first_name', 'last_name', 'password',
'address_1', 'city', 'state', 'zip_code'}
You don't need a lot of parenthesis around your if
statements, so:
if (request.POST):
becomes:
if request.POST:
In the if
statement:
if (not (required <= set(request.POST))):
You can first get rid of the outer parenthesis:
if not (required <= set(request.POST)):
(削除) Instead of saying not <=
you can just do <
:
if set(request.POST) < required:
as it reads:
You have less than you need for the required fields. (削除ここまで)
I would not make so many trivial variables such as:
username = request.POST['username']
Just use request.POST['username']
. You don't use username
, email
... that often, I would just use request.POST['thing']
to save space. Although there are many reasons not to make code as terse as possible, adding lots of these variable declarations makes your code hard to read.
-
\$\begingroup\$ Your first and last points especially were helpful. However, I have to disagree with you on your
if set(request.POST) < required:
statement.request.POST
may contain items which are not inrequired
but I want to check whether or not everything inrequired
is also inrequest.POST
, i.e. whether or notrequired
is a subset ofrequest.POST
. An example of something inrequest.POST
but not inrequired
would be theaddress_2
field, which is not required. \$\endgroup\$Woody1193– Woody11932016年08月01日 17:21:05 +00:00Commented Aug 1, 2016 at 17:21