2017-05-20 11 views
0

4つの引数を持つ関数を定義しようとしていますが、私の第4引数で問題に直面しています。ここに私のコードです:django python:欠落している1つの位置指定引数

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'     
+0

HTMLテンプレートがcreate_recording 'にキーワードスタイルの引数を渡す()':

EDITは: あなたのURLで問題があります。おそらくどちらか一方を変更して、どちらも同じスタイルを使用する必要があります。 –

+0

id = 1の問題は何ですか? id = 1を渡すと、どのようなエラーが表示されますか? – Exprator

答えて

2

あなたの問題は、ここでは、ビューを期待しているcomponent_id送信しません

<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'), 
+0

あなたは正しいです!しかし、これは私がそれを修正した私の質問のちょうどタイプミスでした!今度は正しい – George

+0

なので、あなたの問題はまだ解決されていませんか?その場合は、URLパターンに問題がある可能性があります。 URLパターンを表示するために質問を編集できますか? – karthikr

+0

いいえ!質問にURLを追加しました – George

関連する問題