首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有两个插件的Python-django url

带有两个插件的Python-django url
EN

Stack Overflow用户
提问于 2017-05-18 12:15:41
回答 1查看 1.4K关注 0票数 0

您好,我有一个问题,当我尝试在一个网址中的两个弹头。我有:

html文件:

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
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模式:

代码语言:javascript
复制
url(r'^plants/(?P<plant_slug>[-\w])/(?P<inspection_slug>[-\w]+)/$', views.inspection_detail, name='inspection_detail'),

但我得到了:

代码语言:javascript
复制
Page not found (404)

我看不出哪里是错的!

EN

回答 1

Stack Overflow用户

发布于 2017-05-20 23:27:39

下面是我修复它的方法:

在html文件中:

代码语言:javascript
复制
<h4><a href="{% url 'data:inspection_detail' slug=plant.slug inspection_id=inspection.id %}" >{{inspection.name}}</a></h4>

我的views.py

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording$', views.create_recording, name='create_recording'),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44038482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档