# Profile creation

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):
if (not (required <= set(request.POST))):
return render( request, 'profiles/create.html', { 'missing' : required - set(request.POST) } )
email = request.POST['email']
if (not Profile.is_unique(email)):
first_name = request.POST['first_name']
last_name = request.POST['last_name']
city = request.POST['city']
state = request.POST['state']
zip_code = request.POST['zip_code']
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:

1. Is there any way I could avoid assigning to individual variables from the request.POST calls, maybe using setattr? Would that be faster or slower than doing it this way?
2. 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.

required = set( ['username', 'email', 'first_name', 'last_name', 'password',


You can just use {}:

required = {'username', 'email', 'first_name', 'last_name', 'password',


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:


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.
• 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 in required but I want to check whether or not everything in required is also in request.POST, i.e. whether or not required is a subset of request.POST. An example of something in request.POST but not in required would be the address_2 field, which is not required. – Woody1193 Aug 1 '16 at 17:21