【发布时间】:2017-10-20 15:08:36
【问题描述】:
我正在尝试定义一个有 4 个参数的函数,但我的第四个参数遇到了问题,而且我很确定这很愚蠢。这是我的代码:
views.py:
def create_recording(request, slug, inspection_id, component_id):
inspection = get_object_or_404(Inspection, pk=inspection_id)
plant = get_object_or_404(Plant, slug=slug)
component=get_object_or_404(Component, pk=component_id)
if request.method == "POST":
form = RecordingForm(request.POST)
if form.is_valid():
recording = form.save(commit=False)
recording.plant = plant
recording.inspection=inspection
recording.component=component
recording.save()
return redirect('data:inspection_detail', slug=plant.slug, inspection_id=inspection.id)
else:
form = RecordingForm()
context = {'form': form, 'plant':plant,'inspection':inspection}
template = 'data/create_recording.html'
return render(request, template, context)
这是我的 html 文件:
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=inspection.id component_id=1 %}">
<button type="button" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span> Chaiir
</button>
</a>
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=inspection.id component_id=2 %}">
<button type="button" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span> Table
</button>
</a>
网址:
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording$', views.create_recording, name='create_recording'),
我收到以下错误:
Exception Type: TypeError
Exception Value:create_recording() missing 1 required positional argument: 'component_id'
【问题讨论】:
-
HTML 模板将关键字样式的参数传递给
create_recording(),而函数本身则接受普通的位置参数。您可能应该更改其中一个或另一个,以便它们都使用相同的样式。 -
id=1 有什么问题?传递 id=1 时显示什么错误?