我有一个用户输入位置信息的表单。我正在尝试制作更新表单,以便用户可以单击某个位置上的更新按钮,更新表单中会填充该位置已存在的数据。我已经关注了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>
答案 0 :(得分:7)
我看到一些问题:
如果您打算使用更新视图,您不需要将其包装在另一个视图中或将其用作表单,它完全是独立的。
在 views.py
(不是forms.py
)中:
from .models import Location
from .forms import LocationForm
class LocationUpdateView(UpdateView):
model = Location
template_name = 'locations/location_update.html'
form_class = LocationForm
success_url = '/accounts/loggedin/locations/all/'
在urls.py
:
url('location/update/(?P<pk>\d+)/$', LocationUpdateView.as_view(),
name='location-update'),
如果您像这样连接它,则需要使用要更新的位置对象的主键调用更新视图。这将确保表单填充数据;所以称之为<a href="{% url 'location-update' pk=1 %}">update location id 1</a>
。
如果您不想使用基于类的视图,那么应该更新您的视图以首先加载数据,填充表单,然后将表单发送给用户,就像上面的UpdateView一样,您需要通过在主键(或其他引用)中,加载要编辑记录的对象:
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})