[Django]-How can i generate a page dynamically in Django?

4👍

You could use FBVs. One view and one template required.

Views.py:

from django.shortcuts import get_object_or_404, render

def render_items(request, item_name):
    item = get_object_or_404(YOUR_MODEL, YOUR_ITEM_FIELD_NAME=item_name)
    return render(request, 'YOUR_TEMPLATE.html', {'item': item })

Add below line to urlpatterns

    path('items/<str:item_name>/',views.render_items, name='item'),

Detail Template

{% extends "main/header.html" %}
{% block content %}
<body>
    <div class="item-detail">
        <h1> Detail </h1>
        <p>{{ item.name }}</p>
    </div>
</body>
{% endblock %}

Leave a comment