I have a form that users enter in information on locations. I'm trying to make an update form so users can click the update button on a location and the update form comes populated with the data thats already there for that location. I have followed the Django docs but I am getting a blank screen. I am unsure how to get it to reference which location to update. Here is what I have so far.
models.py
class Location(models.Model): # Details on location views.py
def locations(request): return render_to_response('locations/locations.html', {'locations': Location.objects.all() }) def location(request, location_id=1): return render_to_response('locations/location.html', {'location': Location.objects.get(id=location_id) }) def create(request): if request.POST: form = LocationForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/accounts/loggedin/locations/all/') else: form = LocationForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('locations/create_location.html', args) def edit_location(request): if request.POST: form = LocationUpdate(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/accounts/loggedin/locations/all/') else: form = LocationUpdate() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('locations/location_update.html', args) forms.py
from django.views.generic.edit import UpdateView from models import Location class LocationForm(forms.ModelForm): class Meta: model = Location exclude = ('timestamp', 'updated') class LocationUpdate(UpdateView): model = Location template_name_suffix = '_update_form' url.py
url(r'^accounts/loggedin/location/update/$', 'assessments.views.edit_location'), location_update.html
<!DOCTYPE html> <html> <head> </head> <body> <form action="" method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="update" /> </form> </body> </html> or should it go in location.html. If i add it in there i get a CSRF verification failed error
<div class="row"> <div class="col-md-4"> <dl class="dl-horizontal"> <dt>ID:</dt> <dd>{{ location.id }}</dd> <br /> <dt>Company:</dt> <dd>{{ location.company }}</dd> <dt>Address:</dt> <dd>{{ location.address }}</dd> <dt>Town:</dt> <dd>{{ location.town_city }}</dd> <dt>County:</dt> <dd>{{ location.county }}</dd> <dt>Telephone:</dt> <dd>{{ location.tel }}</dd> ###### ect ##### </div> </div> <form action="" method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="update" /> </form>