Django forms "pattern" for this situation?
I'm pretty new to Python so that may be a stupid question but I'll ask it
anyway. Is there a Django forms "design pattern" for this common view
situation? When I run the view, I want it to act on one of two different
types of forms depending on the type of user who's filling out the form.
It seems ugly to have two if/then blocks inside the if request.method
block to determine which type of form I'm acting on. What I'd like is to
be able to refer to a "CreateProfileForm" that will refer to either a
CreateManProfileForm or CreateWomanProfileForm depending on what's in the
session variable.
Thanks!
def create_profile(request, template):
if request.session['user_type_cd'] == 'man':
is_man = True
else:
is_man = False
if request.method == "POST":
if is_man:
form = CreateManProfileForm(request.POST)
else:
form = CreateWomanProfileForm(request.POST)
if form.is_valid():
# Do stuff
return HttpResponseRedirect(reverse('do-next-thing'))
else:
if is_man:
form = CreateManProfileForm()
else:
form = CreateWomanProfileForm()
return render_to_response(template, locals(),
context_instance=RequestContext(request))
No comments:
Post a Comment