【发布时间】:2018-04-27 16:29:25
【问题描述】:
您好,我正在尝试使用参数创建链接,但出现错误(找不到页面)
错误
Reverse for 'hexcode' with arguments '('#d4cbd0',)' not found. 1 pattern(s) tried: ['hexcode/(?P<color>\\w)$']
模板
{% for color in palette_dominant_color %}
<a href="{% url 'brandcolors:hexcode' color %}" style="text-decoration:none;color:inherit">
{{color}}
</a>
<br>
{% endfor %}
urls.py
url(r'^hexcode/(?P<color>\w)/$', ThemeView.as_view(), name="hexcode"),
views.py
class ThemeView(TemplateView):
template_name='fabric/theme.html'
def get_context_data(self, **kwargs):
context = super(ThemeView, self).get_context_data(**kwargs)
colors = Color.objects.filter(color=kwargs['color']).all()
return context
【问题讨论】:
-
顺便说一句,你的上下文没有添加你需要设置
context['colors'] = Color.... -
请注意,尝试在 URL 中包含
#可能会导致问题,因为它用于 fragment identifiers。反转 URL 就可以了,因为 Django 会将哈希编码为%23。但是,如果您的用户尝试使用 URL/hexcode/#d4cbd0,那么他们的浏览器将剥离锚点,仅将/hexcode/发送到服务器。
标签: django django-templates django-urls