python - Django Update Form? -
i have form users enter in information on locations. i'm trying make update form users can click update button on location , update form comes populated data thats there location. have followed django docs getting blank screen. unsure how reference location update. here have 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 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 go in location.html. if add in there 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>
i see few problems:
- your update view create view.
- you passing updateview class form in update view.
if going use update view, don't need wrap in view or use form, entirely self-contained.
in views.py
(not forms.py
):
from .models import location .forms import locationform class locationupdateview(updateview): model = location template_name = 'locations/location_update.html' form_class = locationform success_url = '/accounts/loggedin/locations/all/'
in urls.py
:
url('location/update/(?p<pk>\d+)/$', locationupdateview.as_view(), name='location-update'),
if wire this, need call update view primary key of location object want update. make sure form populated data; call <a href="{% url 'location-update' pk=1 %}">update location id 1</a>
.
if not want use class based view, view should updated load data first, populate form, send form user, , updateview above, need pass in primary key (or other reference), load object editing record:
def update_location(request, pk=none): obj = get_object_or_404(location, pk=pk) form = locationform(request.post or none, request.files or none, instance=obj) if request.method == 'post': if form.is_valid(): form.save() return redirect('/accounts/loggedin/locations/all/') return render(request, 'locations/location_update.html', {'form': form})
Comments
Post a Comment