def edit_location(request, areacode=None, locationid=None): logger = logging.getLogger('webapp') logger.info('run edit_location run') if(areacode and locationid): area = Area.objects.get(id=int(areacode)) l = Location.objects.get(id=int(locationid)) if request.method == 'POST': #update record with submitted values form = LocationForm(request.POST, instance=l) if form.is_valid(): l.name = form.cleaned_data['name'] l.city = form.cleaned_data['city'] l.state = form.cleaned_data['state'] l.adminEmail = form.cleaned_data['adminEmail'] l.save() return HttpResponseRedirect('/area/' + areacode + '/locations') return render(request, 'area/location_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/' + locationid + '/', 'http_method':'POST', 'area': area}) else: #load record to allow edition form = LocationForm(instance=l) return render(request, 'area/location_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/' + locationid + '/', 'http_method':'POST', 'area': area}) else: return HttpResponseRedirect('/area/' + areacode + '/locations') if areacode else HttpResponseRedirect('/areas/')
def handle_location(request, areacode=None): logger = logging.getLogger('webapp') logger.info('run handle_location run') area = Area.objects.get(id=int(areacode)) if request.method == 'POST': form = LocationForm(request.POST) if form.is_valid(): l = Location() l.name = form.cleaned_data['name'] l.city = form.cleaned_data['city'] l.state = form.cleaned_data['state'] l.adminEmail = form.cleaned_data['adminEmail'] area = Area.objects.get(id=int(areacode)) area.location_set.add(l) return HttpResponseRedirect('/area/' + areacode + '/locations') else: form = LocationForm() return render(request, 'area/location_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/', 'http_method':'POST', 'area': area})