我有一個表單,用戶輸入有關位置的信息。我正在嘗試製作更新表單,以便用戶可以單擊某個位置上的更新按鈕,並使用該位置已存在的數據填充更新表單。我遵循Django文檔,但我得到一個空白屏幕。我不確定如何讓它引用要更新的位置。這是我到目前爲止。Django更新表單?
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>
,或者它應該在location.html去。如果我在那裏添加它,我得到一個CSRF verification failed
錯誤
<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>
這是您的location_update.html的完整代碼嗎?你可能沒有擴展另一個模板? – 2014-09-24 09:29:17
出於測試目的,它只是得到了html標題和body標籤。一旦它正在工作,我會將其添加到模板。它是否需要成爲location.html的一部分? – 2014-09-24 09:31:09
我不明白這個答覆。這是location_update.html的完整代碼嗎?如果沒有,請發佈完整的代碼。另外,請正確解釋*您看到的內容。你有什麼表現嗎?當你查看源代碼時你看到了什麼? – 2014-09-24 09:33:10