Python-django url有两个slu ..

时间:2017-05-18 04:15:41

标签: python django url-pattern

你好,我在一个网址中尝试两个slu in时遇到问题。我有:

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

但我明白了:

Page not found (404)

我无法看出错误在哪里!

1 个答案:

答案 0 :(得分:0)

这就是我修复它的方法:

在html文件中:

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

my views.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(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording$', views.create_recording, name='create_recording'),