コード例 #1
0
ファイル: views.py プロジェクト: har11/djangotest
def editagrofield(request, agrofield_id):
	agrofield = get_object_or_404(AgroField,pk=agrofield_id)
	agrofield_user_id = agrofield.created_by_id
	logged_in_user_id = request.user.id

	if agrofield_user_id != logged_in_user_id:
		raise Http404
	agrofield.updated_at = datetime.datetime.now()
	if request.POST:
		form = AgroFieldForm(request.POST, instance=agrofield)
	
		if form.is_valid():
			p = form.save(commit=False)
			p.updated_by = request.user
			
		try:		
			p.save()
		except IntegrityError:
			form.errors["name"] = ["You cannot register more agrofields with same name"]			

			#In case of edit this extra row was needed for the validation comparing with the validation of the create form. I do not know why yet.
			return render(request,'approba/editagrofield.html',{'form': form},)
		else:		
			return HttpResponseRedirect(reverse('approba:agrofieldlist'))
	else:
		form = AgroFieldForm(instance=agrofield)	
		return render(request,'approba/editagrofield.html',{'form': form},)
コード例 #2
0
ファイル: views.py プロジェクト: har11/djangotest
def newagrofield(request):
	"""Add new agrofield"""
	if request.method == 'POST':
		form = AgroFieldForm(request.POST)
		
		if form.is_valid():
			p = form.save(commit=False)
			p.created_by = request.user
			p.updated_by = p.created_by

			try:
				p.save()
			except IntegrityError:
				form.errors["name"] = ["You cannot register more agrofields with same name"]
			else:
				return HttpResponseRedirect(reverse('approba:agrofieldlist'))
	else:
		form = AgroFieldForm()
	return render(request,'approba/newagrofield.html',{'form': form},)