Django检索图片ID

时间:2013-03-16 04:58:01

标签: django

我正在尝试实现当用户点击白板上的图片时的结果,它会将图片ID的参数发送给我的函数。

这是一个例子。这显示了我的所有用户板,当他点击主板时,它会将用户的主板ID重定向到我的功能

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

我正在尝试使用图像来实现相同的功能。当用户单击图像时。我想用图像id的参数将用户重定向到我的函数。我会知道是否会发生这种情况,因为我的功能会将用户重定向到我的个人资料,但问题是当用户点击图片时,它不会重定向。我认为原因是超链接和图像彼此无关。

如何修复此错误,以便当用户点击图片时,他将被重定向到图片ID的参数

<li><a href ="{% url world:Like pet.id %}"><img src= "{{ pet.image.url }}">

我的views.py

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

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

URLconf.py

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

我的views.py

def Like(request,picture_id):
    everyone = Person.objects.all()
    return render(request,'everyone.html',{'everyone':everyone,'follow':True,})

我希望这有意义,否则我会尝试重写它直到它有意义。谢谢你:]

1 个答案:

答案 0 :(得分:1)

您的</li></a>位置不正确,必须是:

<li>
   <a href ="{% url world:Like pet.id %}">
       <img src= "{{ pet.image.url }}" style="cursor:pointer">
   </a>
</li>

我在您的图片中添加了cursor:pointer

<强>更新

好的,我会追踪你的代码,这就是为什么你的照片因为like and boat has the same url address而无所作为的问题。为什么碰巧是一样的?即使他们有不同的网址名称。请注意浏览器中的上方地址网址,它们都会返回http://localhost:8000/1/

就像网址一样:

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

    //which return http://localhost:8000/1/ --> 1 is just a sample id

船网址是:

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

    //which return also http://localhost:8000/1/ --> 1 is just a sample id

要使其生效并修复,您必须更改其中一个网址如下:

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

    //which return now as http://localhost:8000/like/1/