【发布时间】:2017-05-18 04:15:41
【问题描述】:
您好,我在一个 url 中尝试两个 slug 时遇到问题。我有:
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)
我看不出哪里错了!
【问题讨论】:
-
回溯说什么?它指向哪条线?
-
The current URL, plants/mel-1/{% url plant.get_absolute_url}, didn't match any of these. -
贴出你写get_absolute_url函数的代码
-
class Plant(models.Model): ........... def get_absolute_url(self): return reverse ('data:detail', args=[self.slug]) ......... -
您的
plant_slug捕获组只适合一个字符。
标签: python django url-pattern