[Answer]-Working with links in Django

1👍

It is better to use the id or slug field for such task.

But if you surely want to use the title as the GET parameter then apply the urlencode filter to the field’s value:

<a href="{% url 'post_detail' %}?title={{ singleTile.title|urlencode }}">
    {{ singleTile.title }}
</a>

And the view will be something like this:

def post_detail(request):
    post = get_object_or_404(Post, title=request.GET.get('title'))
    return render(request, 'post_detail.html', {'post': post})

UPDATE: If you decide to go with the id/slug option then you can use the generic DetailView:

<a href="{% url 'post_detail' singleTile.id %}">
    {{ singleTile.title }}
</a

urls.py:

from django.views.generic.detail import DetailView
from app.models import Post

url(r'^post/(?P<pk>\d+)/$', DetailView.as_view(model=Post),
                            name='post_detail')

0👍

You have to configure url first like

<a href="#">{% url 'app.views.post_id' singleTile.id %}</a></li>

In your urls

url(r'^post/(?P<post_id>\d+)/$', views.by_id, name='post_id'),

And in your views

def post_id(request, post_id):
   allTiles = Post.objects.get(id=post_id)
   return render(request, template, context)

Leave a comment