django python:缺少1个必需的位置参数
我试图定义一个有4个参数的函数,但是我的第四个参数遇到了一个问题,我很确定这是一个愚蠢的问题。这是我的密码: views.py:django python:缺少1个必需的位置参数,python,django,Python,Django,我试图定义一个有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) co
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>
你的问题在这里
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=2 %}">
你的问题在这里
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=2 %}">
你是对的!但这只是我问题中的一个输入错误,我改正了!现在它是正确的,所以你的问题仍然没有解决?如果是这样的话,可能是您的URL模式有问题。您是否可以编辑问题以显示url模式?您是否注意到您没有在url模式中捕获组件id?如果不捕获它,视图将不会接收它。因此有例外。检查编辑。它工作,但它不采取组件_id=1我必须添加它!我希望你不要指望别人仅仅说代码不起作用就帮你修复代码?你是对的!但这只是我问题中的一个输入错误,我改正了!现在它是正确的,所以你的问题仍然没有解决?如果是这样的话,可能是您的URL模式有问题。您是否可以编辑问题以显示url模式?您是否注意到您没有在url模式中捕获组件id?如果不捕获它,视图将不会接收它。因此有例外。检查编辑。它工作,但它不采取组件_id=1我必须添加它!我希望您不要指望其他人仅仅说代码不起作用就帮您修复代码?HTML模板将关键字样式的参数传递给
create\u recording()
,而函数本身接受普通的位置参数。您可能应该更改其中一个,以便它们都使用相同的样式。id=1有什么问题?传递id=1时显示的错误是什么?HTML模板将关键字样式参数传递给create_recording()
,而函数本身接受普通位置参数。您可能应该更改其中一个,以便它们都使用相同的样式。id=1有什么问题?传递id=1时显示的错误是什么?
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=2 %}">
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording/(?P<component_id>[\d]+)$', views.create_recording, name='create_recording'),