2013-03-10 234 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