On Dec 3, 9:53 am, Todd Blanchard <[email protected]> wrote:
> K
>
> def edit(request):
> form = None
> if request.method == 'GET':
> try:
> form = IncidentForm(instance = Incident.objects.get(id =
> request.GET['id']))
> except ObjectDoesNotExist:
> raise Http404
> else:
> form = IncidentForm(request.POST)
> if form.is_valid():
> form.save()
> return HttpResponseRedirect('/incidents')
>
> media = form.media
> return render_to_response('incidents/new.html',{'form': form, 'media':
> media, 'view': 'Edit'},context_instance=RequestContext(request))
Here you're only passing the instance parameter when the form is
originally displayed on the GET, but not on the POST. The usual layout
is this:
def edit(request, incident_id):
incident = get_object_or_404(Incident, incident_id)
if request.POST:
form = IncidentForm(request.POST, instance=incident)
if form.is_valid():
form.save()
return HttpResponseRedirect('/incidents/')
else:
form = IncidentForm(instance=incident)
return render_to_response(template, {'form': form})
Note it's more Django-nic (that isn't a word, but it should be) to
pass the ID to edit as part of the URL (eg edit/4/), rather than as a
GET parameter.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.