本文整理汇总了Python中django.template.response.TemplateResponse.content方法的典型用法代码示例。如果您正苦于以下问题:Python TemplateResponse.content方法的具体用法?Python TemplateResponse.content怎么用?Python TemplateResponse.content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.response.TemplateResponse
的用法示例。
在下文中一共展示了TemplateResponse.content方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: productEdit
# 需要导入模块: from django.template.response import TemplateResponse [as 别名]
# 或者: from django.template.response.TemplateResponse import content [as 别名]
def productEdit(request, id):
if not request.user.is_active or not request.user.is_superuser:
raise Http404 #filter other users
try:
id = int(id)
product = models.Products.objects.get(id=id)
except:
raise Http404 #bad id or product with this id is not exist
form = None
if request.method == 'POST':
response = {}
saved = False
form = forms.ProductForm(request.POST, request.FILES, instance=product)
#check form
if form.is_valid():
form.save()
response['status'] = 'ok'
saved = True
else:
response['status'] = 'error'
context = RequestContext(request, { 'product':product, 'form':form, 'saved':saved })
response['form'] = template.loader.get_template('form.html').render(context)
response['name'] = product.name
resp = TemplateResponse(request, '')
resp.content = json.dumps(response)
return resp
#return HttpResponse(json.dumps(response))
if request.method == 'GET':
#create form
form = forms.ProductForm(instance=product)
return TemplateResponse(request, 'edit.html', {"form": form, 'product': product})