用户可以在此网址创建飞机贴子:
url(r'^upload/aircraft/$', aircraft_create, name="aircraft_create"),
我已经创建了一个摘要页面,其中显示了所有用户帖子。他们可以在这里编辑和删除他们的帖子。网址:
url(r'^account/uploads/$', upload_overview, name="account_uploads"),
但是,我希望用户能够在其摘要页面上编辑他们的帖子。我现在设置它的方式是,他们可以在upload/aircraft/edit
进行修改,但我想成为account/uploads/edit
。
我已经设置了这样但它没有做任何事情?关于它可能是什么的任何线索?
飞机/ views.py
def aircraft_create(request):
form = aircraft_form(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
messages.success(request, "Your upload has been successfully added!")
return HttpResponseRedirect(instance.get_absolute_url())
else:
messages.error(request, "There seems to be something wrong. Have a look again..!")
context = {"form":form,}
return render(request,'aircraft/aircraft_form.html', context)
更新视图
def aircraft_update(request, id=None):
aircraft = get_object_or_404(Aircraft, id=id)
form = aircraft_form(request.POST or None, instance=aircraft)
if form.is_valid():
form.save()
messages.success(request, "Your edit has been successfully been saved!")
return HttpResponseRedirect(aircraft.get_absolute_url())
return render(request,'aircraft/aircraft_form.html',
{"aircraft": aircraft, "form": form})
模板
{% if UploadedAircraft %}
{% for upload in UploadedAircraft %}
<div class="col-lg-offset-0 col-md-4 col-sm-3 item">
<div class="box"><a href="{{ upload.get_absolute_url }}"><img src="{{ upload.image.url }}" width="200px" height="200px" alt="{{ upload.title }}"/></a>
<h3 class="name"><a href="{{ upload.get_absolute_url }}">{{ upload.name }}</a></h3>
<a href="{% url 'aircraft_update' %}"><button class="btn">Edit</button></a>
<a href="{% url 'aircraft_delete' %}"><button class="btn">Delete</button></a>
</div>
摘要页面视图
def upload_overview(request):
uploaded_aircraft = Aircraft.objects.filter(user=request.user)
return render(request,'account/upload_overview.html',{'UploadedAircraft':uploaded_aircraft)
url.py
#aircraft/detail/1
url('^aircraft/detail/(?P<id>\d+)/$', aircraft_detail, name='aircraft_detail'),
#account/uploads (Display Users uploads)
url(r'^account/uploads/$', upload_overview, name="account_uploads"),
#upload/aircraft (Create Aircraft)
url(r'^upload/aircraft/$', aircraft_create, name="aircraft_create"),
#Edit/aircraft
url('^account/uploads/$', aircraft_update, name='aircraft_update'),
修改
def airline_update(request, id=None):
airline = get_object_or_404(Airline, id=id)
form = airline_form(request.POST or None, instance=airline)
if form.is_valid():
form.save()
return HttpResponseRedirect(airline.get_absolute_url())
return render(request,'airline/airline_form.html',
{"airline": airline, "form": form})
网址
#account/upload/edit/ (Edit airline)
url(r'^account/uploads/edit/(?P<id>[0-9]+)$', airline_update, name='airline_update'),
模板
<a href="{% url 'airline_update' id=upload.id %}"><button class="btn">Edit</button></a>
答案 0 :(得分:2)
假设您正在尝试编辑具有特定ID的飞机,您需要在url.py中使用类似的内容(假设ID是整数):
url('^account/uploads/edit/(?P<id>[0-9]+)$', aircraft_update, name='aircraft_update')
在您的模板中,您需要更新您的锚链接以包含ID:
<a href="{% url 'aircraft_update' id=upload.id %}">
请注意,这假定upload
对象(在模板的循环中)包含id
属性,id
属性对应于您要更新的飞机ID。 (您可能已将此属性命名为其他内容。)
编辑:这足以执行GET请求。但是,我注意到aircraft_update
的视图定义也会尝试检查表单是否有效。模板中的这个表单在哪里?它似乎不在您的循环中。