Is it possible to customize individual pages associated with the same template? dynamic URL django


I am working on a blog website and have set up dynamic URLs in Django for each blog article. Is it possible to change the layout of paragraphs and images of specific pages so that there are slight differences between each page?


Yes it is possible to have different layout for the same template rendered in Django. You can pass some variable in context to achieve this goal like this :

def detail(request, id):
    object = Model.objects.get(pk=id)
    context = {'layout': f"layout_for_{id}"}
    return render(request, 'template.html', context)


{% if layout == 'layout_for_2' %}
Layout for 2 here
{% endif %}

You can add more condition as you want, but it can become hard to for too much {% if %} block.

Answered By – Rvector

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published