html - Working with links in Django -
i working in small blog application using django. sorry if question obvious, newbie. third since started online course. have following queryset:
def all(request): alltiles = post.objects.values('title') allposts = post.objects.all()[:3] context = {'posts': allposts,"titles":alltiles} template = "home.html" return render(request, template, context)
and follwing html code:
<ol class="list-unstyled"> {% singletile in titles %} <li><a href="#">{{singletile.title}}</a></li> {% endfor %} </ol>
as can see every title creates link. lets assume person decides read 1 of posts. how can use title name , send request database content of post.
it better use id
or slug
field such task.
but if surely want use title
parameter apply urlencode filter field's value:
<a href="{% url 'post_detail' %}?title={{ singletile.title|urlencode }}"> {{ singletile.title }} </a>
and view 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 decide go id
/slug
option can use generic detailview:
<a href="{% url 'post_detail' singletile.id %}"> {{ singletile.title }} </a
urls.py:
from django.views.generic.detail import detailview app.models import post url(r'^post/(?p<pk>\d+)/$', detailview.as_view(model=post), name='post_detail')
Comments
Post a Comment