示例#1
0
def create_product(request):
	if not request.user.is_staff:
		return HttpResponseForbidden()

	if request.method == 'GET':
		form = ProductForm()
	else:
		form = ProductForm(request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect('/onlineStore/')

	args = {'form' : form}
	args.update(csrf(request))
	return render_to_response('onlineStore/create_product.html',args, context_instance=RequestContext(request))
示例#2
0
def edit_product(request, instance_id):
	if not request.user.is_staff:
		return HttpResponseForbidden()

	this_product = get_object_or_404(Product,pk=instance_id)
	if request.method == 'GET':
		form = ProductForm(initial ={'name':this_product.name, 'price':this_product.price, 'stock_quantity':this_product.stock_quantity, 'description':this_product.description})
	elif request.method == 'POST':
		form = ProductForm(request.POST, instance=this_product)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect('/onlineStore/')

	args = { 'form' : form , 'product':this_product }
	args.update(csrf(request))
	return render_to_response('onlineStore/edit_product.html',args, context_instance=RequestContext(request,instance_id))