Answers for "pages in django"

1

django dynamic pages

# 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 %}
Posted by: Guest on September-14-2020
0

add page to django

Example (Hope this helps)

1) url.py
path('po/comparison/<int:pk>/', views.po_comparison, name='po_comparison'),

2) view.py
@login_required
def po_comparison(request, pk):

    po = get_object_or_404(PurchaseOrder, pk=pk)
    po_details = PurchaseOrderDetail.objects.filter(po=po).order_by("id")

    return render(request, 'po/comparison.html', {'po': po, 'po_details': po_details})

3) comparison.html
Your HTML Page goes here
Posted by: Guest on August-28-2020

Python Answers by Framework

Browse Popular Code Answers by Language