Reputation: 7635
I have a view that display a form that can be saved with a submit button. I dont want the view to be reloaded when the form is submit. However, I have an error if the function do not return a response.
view.py
from django.shortcuts import render_to_response
from ezmapping.models import *
from django.forms.models import modelformset_factory
def setAppOptions(request, map_name):
if request.user.is_authenticated():
app_selected = EzApp.objects.get(app_name=app_name, created_by=request.user)
formset = ezAppOptionFormSet(user=request.user, instance=app_selected)
if request.method == 'POST':
formset = ezAppOptionFormSet(request.POST, instance=app_selected, user=request.user)
if formset.is_valid():
formset.save()
return render_to_response("manage_app_options.html", {'formset': formset}, context_instance=RequestContext(request))
else:
error_msg = u"You are not logged in"
return HttpResponseServerError(error_msg)
Upvotes: 2
Views: 1219
Reputation: 34553
You won't be able to do the POST without re-rendering the template, unless you're posting the form via ajax. If you submit the form via ajax and get a response back that is JSON, XML, etc, you can then update the template with the form errors or other information.
However, given the use case outlined in your view, I would suggest limiting access to the view altogether using the @login_required decorator. If you do that, your view logic can be greatly simplified.
Upvotes: 2