コード例 #1
0
def editSegment(request, seg_id):
	# get segment details
	s = get_object_or_404(Segment, pk=seg_id)
	
	# check to see if edit form has been submitted
	if request.POST:
		# bind form
		form = SegmentForm(request.POST)
		
		# validate form
		if form.is_valid():
			# save cleaned data from form
			s.name = form.cleaned_data['name']
			s.description = form.cleaned_data['description']
			s.save()
			
			# return to segment details page
			return HttpResponseRedirect('/segment/'+seg_id)
	else:
		# gather initial data
		data = {
			'name' : s.name,
			'description' : s.description
		}
		# initialize unbound form
		form = SegmentForm(initial=data)
	
	segment_list = Segment.objects.all().order_by('name')
	recent_news = Source.objects.all().order_by('-modified')[:5]
	return render_to_response('market/segment/edit.html',{'form':form,'recent_news':recent_news,'segment_list':segment_list,'s':s},context_instance=RequestContext(request))
コード例 #2
0
def addSegment(request):
	if request.POST:
		# bind form to post data
		form = SegmentForm(request.POST)
		
		# validate form
		if form.is_valid():
			seg = Segment(name=form.cleaned_data['name'])
			seg.save()
			return HttpResponseRedirect('/segment/'+str(seg.id))
	else:
		form = SegmentForm() # load unbound form
	
	segments = Segment.objects.all().order_by('name')
	recent_news = Source.objects.all().order_by('-modified')[:5]
	return render_to_response('market/segment/add.html', {
		'form':form,'recent_news' : recent_news, 'segments' : segments
		},
		context_instance=RequestContext(request)
	)