Reputation: 110093
How would I break the following view function into two: one for the Add
action and one for the Delete
action.
# in urls.py
urlpatterns += patterns('myproject.views',
url(r'^profile/edit/education/$', 'edit_education', name='edit_education'),)
# in views.py
@login_required
def edit_education(request):
if request.method == 'POST':
if 'Delete' in request.POST.values():
profile.educations.remove(Education.objects.get(id=education_id))
return redirect('edit_education')
if 'Add School' in request.POST.values():
form = EducationForm(request.POST)
if form.is_valid() and request.POST['school']:
form.save()
return redirect('edit_education')
else:
form = EducationForm()
return render_to_response('userprofile/edit_education.html', {'form': form}, context_instance=RequestContext(request))
What changes would I need to make in these two files to break the view into two separate functions? Thank you.
Upvotes: 0
Views: 94
Reputation: 599470
Why do you want to do this? That code is fine - compact and self-explanatory.
Splitting it up would require not only changes in the view, but (obviously) changes in the form template so that delete
would post somewhere different from add
. There's no easy way of doing that, other than having two separate <form>
s, or using some Javascript to change the form's action
depending on which button you press. Doesn't seem like it's worth it.
Upvotes: 1