donkeyboy72
donkeyboy72

Reputation: 1943

Django Retrieving Picture ID

I'm trying to achieve the results of when a user clicks on a picture from the whiteboard, it will send an argument of the picture id to my function.

This is an example .This display all my users board and when he clicks on the board it will redirect the user with the board id to my function

<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 %}

I'm trying to achieve the same thing but with an image. When a users click on an image . I want to redirect the user with the argument of the image id to my function. I will know if it happens because my function will redirect the users to my profile but the problem is when the user clicks on the picture , it does not redirect. I think the reason is the hyperlink and the image ain't relating with to each other.

How can I fix this error so when a user clicks on a picture , he will be redirected with the argument of the picture ID

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

My 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'
    ),

My views.py

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

I hope this make sense otherwise i'll try to rewrite it until it make sense.thank you :]

Upvotes: 0

Views: 231

Answers (1)

catherine
catherine

Reputation: 22808

Your </li> and </a> is in the incorrect position, it must be:

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

I added cursor:pointer in your image

Update:

OK I trace your codes, the problem why your picture do nothing because the like and boat has the same url address. Why it happen to be the same? even though they have different url name. Notice the address url above in your browser, they both return http://localhost:8000/1/.

Like url is:

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

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

Boat url is:

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

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

To make it effective and fix, you must change the url address either one of them like this:

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

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

Upvotes: 1

Related Questions