0

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>
Kryštof Řeháček
  • 1,965
  • 1
  • 16
  • 28
killerbees
  • 209
  • 1
  • 6
  • 19
  • You said you want your URL to be like this `localhost/category/subcategory/..` but you have `localhost/...` simply change your url patterns! or your problem is that you don't know how to do this? – mohammedgqudah Feb 09 '18 at 17:05
  • mohammad thats why I asking. I don't know how to do category/subcategory/detail_view.html. How I wrote, now I have only localhost/detail_view.html – killerbees Feb 09 '18 at 17:07
  • ok use this `path('^category/subcategory/', views.detale_firmy, name='detale_firmy'),` instead – mohammedgqudah Feb 09 '18 at 17:09
  • 1
    do you want `localhost/category/subcategory` or `localhost/123/3333`, where 123 is a category and 3333 a subcategory? – Walucas Feb 09 '18 at 17:10
  • Walucas yes i want to category and subcatory be getting a slug. How you wrote. – killerbees Feb 09 '18 at 17:12
  • Update your post to make it clear please – Walucas Feb 09 '18 at 17:16
  • 1
    Take a look at https://stackoverflow.com/questions/44038482/python-django-url-with-two-slugs – Walucas Feb 09 '18 at 17:18

1 Answers1

0

I suggest you have the views.py and urls.py in the app, if not, then in your main urls add line to the urlpatterns like this:

( let me use for the app name, your views.py are in, the app_firma name for example )

/project_name/urls.py:

from app_firma.views import detale_firmy

urlpatterns = [
    path('admin/', admin.site.urls),
    ...
    path('<slug:category_slug>/<slug:subcategory_slug>/', detale_firmy, name='detale_firmy'),
    ...
]

and in your /app_firma/views.py change the method definiton to:

def detale_firmy(request, category_slug, subcategory_slug):

    ... use the category and subcategory slugs there ...

    return render(request, 'ogloszenia/detale_firmy.html', {'detale_firmy':detale_firmy}) 

Now you will be able to access the detale_firmy at localhost:8000/<category_slug>/<subcategory_slug>/ even without specifying detail_page.html at the end.

Kryštof Řeháček
  • 1,965
  • 1
  • 16
  • 28