首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django python:缺少1个必需的位置参数

django python:缺少1个必需的位置参数
EN

Stack Overflow用户
提问于 2017-05-20 23:39:18
回答 1查看 3.1K关注 0票数 0

我正在尝试定义一个有4个参数的函数,但我的第四个参数遇到了问题,我非常确定它是愚蠢的。下面是我的代码:

views.py:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording$', views.create_recording, name='create_recording'),

我得到了以下错误:

代码语言:javascript
复制
Exception Type: TypeError
Exception Value:create_recording() missing 1 required positional argument: 'component_id'                  
EN

回答 1

Stack Overflow用户

发布于 2017-05-20 23:40:36

你的问题在这里

代码语言:javascript
复制
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=2 %}">

您没有发送视图所期望的component_id

编辑:您的URL中存在问题:

代码语言:javascript
复制
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording/(?P<component_id>[\d]+)$', views.create_recording, name='create_recording'),
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44087786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档