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

32 Views Asked by At

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?

1

There are 1 best solutions below

0
Rvector On BEST ANSWER

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 :

views.py

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

template.html

{% 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.