我正在尝试定义一个有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:
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' 发布于 2017-05-20 23:40:36
你的问题在这里
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=2 %}">您没有发送视图所期望的component_id。
编辑:您的URL中存在问题:
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording/(?P<component_id>[\d]+)$', views.create_recording, name='create_recording'),https://stackoverflow.com/questions/44087786
复制相似问题