2013-03-10 231 views
2

我正在研究一個簡單的應用程序,在這個應用程序中,寵物用戶可以爲他們的寵物創建電路板並在電路板上顯示圖片。Django NoReverseMatch at/display/

我正試圖創建一個功能,用戶可以點擊他們的板子,將他們重定向到他們的板上,這將顯示他們所有的寵物圖片。

enter image description here

我得到一個錯誤,當我嘗試配置我的模板的用戶重定向到其董事會。

Reverse for 'Boat' with arguments '('',)' and keyword arguments '{}' not found. 
Django Version: 1.4.3 
Exception Type: NoReverseMatch 
Exception Value: Reverse for 'Boat' with arguments '('',)' and keyword arguments '{}' not found. 

Error during template rendering 
In template C:\o\mysite\pet\templates\edit.html, error at line 14 
13  {% for b in board %}   

14  <li><a href ="{% url world:Boat animal.id %}">{{ b.name }}</li> 

enter image description here

我覺得問題就在這裏 當在船上的用戶點擊。我的URLconf將捕獲animal.id並把它傳遞到我的船的功能,但我不知道哪裏出了問題

<li><a href ="{% url world:Boat animal.id %}">{{ b.name }}</li> 

我edit.html

<h4>My Boards</h4> 
{% if board %} 
<ul> 
    {% for b in board %}   
    <li><a href ="{% url world:Boat animal.id %}">{{ b.name }}</li> 
    {% endfor %} 
</ul> 
{% endif %} 

我的主URL配置

urlpatterns = patterns('', 
    url(
     r'^admin/', 
     include(admin.site.urls) 
    ), 
    url(
     r'^', 
     include('pet.urls', 
      namespace = 'world') 
    ), 

我的寵物應用的部分URLconf

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

我的我的views.py的部分

def Profile(request): 
    if not request.user.is_authenticated(): 
     return HttpResponseRedirect(reverse('world:LoginRequest')) 

    if request.method == 'POST': 
     form = PersonForm(request.POST, request.FILES) 
     if form.is_valid(): 
      person = Person.objects.get(user=request.user) 
      person.image = form.cleaned_data['image'] 
      person.name = form.cleaned_data['name'] 
      person.save() 
    return render(request,'profile.html',{'form': PersonForm()}) 

def Boat(request ,animal_id): 
    if not request.user.is_authenticated(): 
      return HttpResponseRedirect(reverse('world:LoginRequest')) 

    picture = Picture.objects.filter(board=animal_id) 
    return render(request,'boat.html',{'picture':picture}) 

謝謝你對我的幫助:]

我更我的views.py

def Display(request): 
    if not request.user.is_authenticated(): 
     return HttpResponseRedirect(reverse('world:LoginRequest')) 
    board = Board.objects.filter(user=request.user) 
    person = Person.objects.get(user=request.user) 
    return render(request,'edit.html',{'board':board ,'person':person}) 

的我models.py

class Person(models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=100) 
    image = models.FileField(upload_to="images/",blank=True,null=True) 
    following = models.ManyToManyField('self', related_name='followers', symmetrical=False, blank=True, null=True) 

    def __unicode__(self): 
     return self.name 

class Board(models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=100) 
    def __unicode__(self): 
     return self.name 
+0

你肯定在此行'{%URL世界:船animal.id%}','animal.id'具有價值?它似乎反向無法找到該網址的參數。 – 2013-03-10 05:30:33

+0

此外,您在您的url conf上使用了一個命名參數,但是將調用的位置參數傳遞給模板上的'url'標記。 – asermax 2013-03-10 05:58:01

+0

我可以看到您的主板型號嗎? – catherine 2013-03-12 08:33:46

回答

2

你不必爲animal.id值

{% if board %} 
<ul> 
    {% for b in board %}   
    <li><a href ="{% url world:Boat b.id %}">{{ b.name }}</li> 
    {% endfor %} 
</ul> 
{% endif %} 
+0

這是作品謝謝你:] – donkeyboy72 2013-03-12 10:24:17

1

看起來,在您的模板中,值animal.id未解析。

因爲變量是空的,所以你的URL正則表達式^(?P<animal_id>\d+)/$不匹配。正則表達式指定一個數字,並傳入一個空字符串。

至於爲什麼它沒有解決,我們需要看到渲染'edit.html'的視圖。

+0

我發佈了更多的我的意見 – donkeyboy72 2013-03-10 06:09:18

+0

我怎麼能使animal.id成一個數字並傳遞到我的URL正則表達式? – donkeyboy72 2013-03-10 06:29:30

+0

它看起來像'動物'是董事會?所以用{%url world:Boat board.id%}交換{%url world:Boat animal.id%} – 2013-03-10 07:00:54