2017-09-21 48 views
1

我有一個模板,顯示發言人名稱列表,我想要點擊這些網址並進入發言者頁面,我通過一個唯一的ID看起來是這樣的 「832fc2ca-787f-484B-B815-f82959607311」,問題是,我得到的錯誤Django有一個意想不到的關鍵字參數'id_speaker'

get_speaker_by_id()得到了一個意想不到的關鍵字參數 'id_speaker'

我覺得問題在urls.py或speakers.html href中是ether。我看着每一個相關的問題,但我無法提交解決方案。

urls.py

url(r'^speakers', views.speakers_list, name='speakers'), 
url(r'^speaker/(?P<id_speaker>[0-9a-z\-]+)/$', 
    views.get_speaker_by_id, name='get_speaker_by_id_url') 

這是是揚聲器名稱的列表

speakers.html

{% for speaker in all_speakers_list%} 

<div class="container"> 
    <div class="row"> 
    <div class="col-sm-6"> 
     <a href="{% url 'get_speaker_by_id_url' speaker.speaker_id %}">{{ speaker.greek_name }}</a> 
    </div> 
    </div> 
</div> 
{% endfor %} 

views.py

get_speaker_by_id(request, id_speaker): 
    ''' Get speaker info by id ''' 
    requested_speaker = Speaker.objects.get(speaker_id=id_speaker) 
    template = loader.get_template('greekparliament/speaker.html') 

    context = { 
     'requested_speaker': requested_speaker 
    } 

    return HttpResponse(template.render(context, request)) 

模型模板.py

class Speaker(models.Model): 
    speaker_id = models.CharField(max_length=200, default='') 
    english_name = models.CharField(max_length=200, default='', blank=True) 
    image = models.CharField(max_length=200, default='', blank=True) 
    email = models.EmailField(max_length=254, default='', blank=True) 
    wiki_el = models.URLField(max_length=200, default='', blank=True) 
    twitter = models.URLField(max_length=200, default='', blank=True) 
    greek_name = models.CharField(max_length=200, default='') 
    wiki_en = models.URLField(max_length=200, default='', blank=True) 
    website = models.URLField(max_length=200, default='', blank=True) 
+1

你可以確定你還沒有在views.py文件中多次定義'get_speaker_by_id()'嗎?您可能有多次定義'get_speaker_by_id()'的可能性,並且在第二次您沒有定義任何'id_speaker'參數 –

+0

是的!我不相信我犯了這個錯誤。非常感謝你。也許發佈您的這個答案,以便我可以將其標記爲解決方案? – kpapadop

回答

1

當我們看到下面的錯誤

get_speaker_by_id() got an unexpected keyword argument 'id_speaker' 

,我們可以清楚地看到,它是尋找一個名爲id_speaker參數。

從您發佈的views.py文件的代碼中,我可以清楚地看到參數id_speaker已通過。因此,只有一件事情可能會生成該錯誤,即您已在views.py文件中定義了功能get_speaker_by_id()多於一次

P.S. 通過實施最佳實踐的以下代碼更改函數get_speaker_by_id()的代碼。

from django.shortcuts import render 

... 

def get_speaker_by_id(request, id_speaker): 
    """ 
    Get speaker info by id <You should always write doc> 
    For example, you can follow numpy guidelines on how to 
    write docs - https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt  

    Parameters 
    ---------- 
    request 
     Django request that will come from http 

    id_speaker: str 
     Id of the speaker 

    """ 
    # I think you should handle `DoesNotExist` error here or it will crash 
    # with an Internal Server Error when id_speaker does not exists in db 
    requested_speaker = Speaker.objects.get(speaker_id=id_speaker) 

    context = { 
     'requested_speaker': requested_speaker 
    } 

    return render(request, "greekparliament/speaker.html", context) 
相關問題