开发者

Get the currently saved object in a view in Django

开发者 https://www.devze.com 2022-12-26 12:23 出处:网络
I has a Django view which is accessed through an AJAX call. It\'s a pretty simple one — all it does is simply pass the request to a form object and save the data. Here\'s a snippet from my view:

I has a Django view which is accessed through an AJAX call. It's a pretty simple one — all it does is simply pass the request to a form object and save the data. Here's a snippet from my view:

form = AddSiteForm(request.user, request.POST)
if form.is_valid():
    obj = form.save(commit=False)
    obj.user = request.user
    obj.save()
    data['status'] = 'suc开发者_运维知识库cess'
    data['html'] = render_to_string('site.html', locals(), context_instance=RequestContext(request))
    return HttpResponse(simplejson.dumps(data), mimetype='application/json')

How do I get the currently saved object (including the internally generated id column) and pass it to the template?

Any help guys?

Mridang


obj is the currently saved object (created when you call form.save, and obj.id is the id. It's already passed in locals()

This all may seem obvious, but it's all I could decipher from your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消