2013-03-30 39 views
0

我有形式已由django生成,我試圖返回一個對象id與形式的函數。Django反向爲''與參數'(1,)'和關鍵字參數'{}'沒有找到

我收到這個錯誤。我似乎無法弄清楚爲什麼它不起作用因爲圖片ID是有效的ID和URL的正則表達式應該捕獲它並將其返回給我的函數,除非當前的URL必須匹配因爲我對網頁當前的URL是1:8000 /評論/ 1/

Reverse for 'world.CommentCreator' with arguments '(1,)' and keyword arguments '{}' not found. 

File "C:\o\17\mysite\pet\views.py" in CommentCreator 
    269.  return render(request,'commentcreator.html', {'comment':comment,'picture':p,'search':CommentsForm()}) 

我views.py

def CommentCreator(request,picture_id): 
    p = Picture.objects.get(pk=picture_id) 
    comment = Comment.objects.filter(picture=p) 

    return render(request,'commentcreator.html', {'comment':comment,'picture':p,'search':CommentsForm()}) 

我URL.py

url(
     r'^comment/(?P<picture_id>\d+)/$', 
     'pet.views.CommentCreator', 
     name = 'CommentCreator', 
    ), 

的Html

{% if picture.image %} 
{% endif %} 
<ul>   
    <br><img src="{{ picture.image.url }}"> 

    </ul> 

{% for c in comment %} 
    {% ifequal c.picture.id picture.id %} 

<br>{{ c.body }}</li> 
<br>{{ c.created}}</li> 
<br>{{ c.user}}</li> 
    {% endifequal %} 

{% endfor %} 

<br><br><br><br> 

{% if picture %} 
<form method = "post" action"{% url world.CommentCreator picture.id %}">{% csrf_token %} 
    {{search}} 
    <input type ="submit" value="Search "/> 
</form> 

{% endif %} 
+0

嘗試引用,並使用你給它'{%URL 'CommentCreator' picture.id%}' – czarchaic

回答

0

你把.,而不是:在url

<form method = "post" action"{% url world.CommentCreator picture.id %}"> 
    {% csrf_token %} 

它必須是

<form method = "post" action"{% url world:CommentCreator picture.id %}"> 
    {% csrf_token %} 
+0

是的,這是可能的,我讀了一個 – catherine

0

你的URL配置使用關鍵字參數爲CommentCreator視圖,它們提供給url這樣:你需要使用URL標記中的URL名稱

{% url 'CommentCreator' picture_id=picture.id %} 
+0

我得到這個錯誤反向的 'world.CommentCreator' 的名稱參數'()'和關鍵字參數'{'picture_id':1}'找不到。當前網址是否必須與Commentcreator網址匹配? – donkeyboy72

+0

固定 - 您需要引用它,然後纔可以引用按名稱的視圖。 – Koterpillar

3

{% url CommentCreator picture.id %} 

就是這樣,如果你在django < 1.3上,則不需要單獨引用url名稱。它仍然在django 1.4中工作,但它被棄用,它在django 1.5中完全刪除。

爲了將來的兼容性,應使用此method如果Django的< 1.5:

{% load url from future %} 
{% url 'CommentCreator' picture.id %} 

對於命名的捕獲組URL的,沒有必要通過網址參數的關鍵字或指定參數時,都將工作(但它是知道的順序很重要,這就是爲什麼關鍵字PARAMS在URL標記更理想):

{% load url from future %} 
{% url 'CommentCreator' picture.id %} 
{% url 'CommentCreator' picture_id=picture.id %} 
相關問題