URL-адрес Python-django с двумя слагами

Привет, у меня проблема, когда я пытаюсь использовать два слага в одном URL-адресе. У меня есть:

HTML-файл:

<div class="panel-heading">
   <h4><a class="link" href="{% url 'data:inspection_detail'
                                plant_slug=plant.slug
                                inspection_slug=inspection.slug%}">{{inspection.name}}</a></h4>
</div>

файл views.py

def inspection_detail(request, inspection_slug, plant_slug):
    if not request.user.is_authenticated():
        return render(request, 'data/login.html')
    else:
        user = request.user
        plant = get_object_or_404(Plant, slug=plant_slug)
        inspection = get_object_or_404(Inspection, slug=inspection_slug)
        template = 'data/inspection_detail.html'
        context = {'inspection': inspection, 'user': user, 'plant':plant}
        return render(request, template, context)

и мои шаблоны URL:

url(r'^plants/(?P<plant_slug>[-\w])/(?P<inspection_slug>[-\w]+)/$', views.inspection_detail, name='inspection_detail'),

Но я получаю:

Page not found (404)

И я не вижу, где ошибка!


person George    schedule 18.05.2017    source источник
comment
Что говорит трассировка? На какую линию он указывает?   -  person Jon Kiparsky    schedule 18.05.2017
comment
опубликуйте код, в котором вы написали функцию get_absolute_url   -  person Exprator    schedule 18.05.2017
comment
Ваша группа захвата plant_slug подходит только для одного персонажа.   -  person Klaus D.    schedule 18.05.2017


Ответы (1)


Вот как я это исправил:

в html-файле:

<h4><a href="{% url 'data:inspection_detail' slug=plant.slug inspection_id=inspection.id %}" >{{inspection.name}}</a></h4>

мои взгляды.py

def inspection_detail(request,slug, inspection_id):
    inspection = get_object_or_404(Inspection, pk=inspection_id)
    plant = get_object_or_404(Plant, slug=slug)
    recordings = Recording.objects.filter(inspection__id=inspection_id)
    template = 'data/inspection_detail.html'
    context = {'plant': plant, 'inspection': inspection, 'recordings':recordings,}
    return render(request, template, context)

и мой файл URL:

url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording$', views.create_recording, name='create_recording'),
person George    schedule 20.05.2017