I want to create detail path in Django. I've create a categories and subcategories directory and after that I putting in subcategory the post. I want to create my path like localhost/category/subcategory/detail_page.html where category is slug of category and subcategory is a slug of subcategory. In that moment my app creating the path like localhost/detail_page.html How to do it?
views.py:
from django.shortcuts import render, get_object_or_404
from .models import Kategoria, Firma
def strona_glowna(request):
kategorie = Kategoria.objects.filter(parent=None).order_by('name')
firmy = Firma.objects.all().order_by('publish')[:5]
context = {'kategorie': kategorie, 'firmy': firmy}
return render(request, 'ogloszenia/index.html', context=context)
def detale_firmy(request, slug):
detale_firmy = get_object_or_404(Firma, slug=slug)
return render(request, 'ogloszenia/detale_firmy.html', {'detale_firmy':detale_firmy})
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.strona_glowna, name='strona_glowna'),
path('<slug>', views.detale_firmy, name='detale_firmy'),
]
index.html
{% for kategoria in kategorie %}
<li>
<b>{{kategoria.name}}</b>
{% if kategoria.children.count > 0 %}
<ul>
{% for sub in kategoria.children.all %}
<li>{{ sub.name }}</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
<div class="col-2">
Ostatio dodane:<br>
{% for firma in firmy %}
<strong>{{firma.title}}</strong><br>
<a href="{% url 'detale_firmy' slug=firma.slug %}"><img src="http://free.pagepeeker.com/v2/thumbs.php?size=m&url={{firma.www}}"/><br></a>