Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django-在forms.ValidationError()中使用urlpattern名称_Django_Django Urls - Fatal编程技术网

django-在forms.ValidationError()中使用urlpattern名称

django-在forms.ValidationError()中使用urlpattern名称,django,django-urls,Django,Django Urls,我有以下声明 raise forms.ValidationError({'product_type': [mark_safe('Product type <a href="/group/detail/%d/">N/A already exists</a> for this combination.' % na[0].product_group.id) ]}) raiseforms.ValidationError({'product\u type': [mark_

我有以下声明

raise forms.ValidationError({'product_type':
    [mark_safe('Product type <a href="/group/detail/%d/">N/A already exists</a> for this combination.' % na[0].product_group.id) ]})
raiseforms.ValidationError({'product\u type':
[mark_-safe('此组合的产品类型''%na[0].产品组.id]})
此应用程序具有以下命名url

url(r'^detail/(?P<pk>[0-9]+)/$', views.ProductGroupDetail.as_view(), name='group_detail'),
url(r'^detail/(?P[0-9]+)/$),views.ProductGroupDetail.as_view(),name='group_detail'),
有没有办法在href中使用
{%url'组\u详细信息“%}
格式而不是硬编码的url

谢谢。

您可以使用函数结果:

url = reverse('group_detail', kwargs={'pk': na[0].product_group.id})
[mark_safe('Product type <a href="%s">N/A already exists</a> for this combination.' % url ]})
url=reverse('group\u detail',kwargs={'pk':na[0]。product\u group.id})
[mark_-safe('此组合的产品类型''%url]})

使用
反向

from django.core.urlresolvers import reverse

url = reverse('group_detail', args=[pk])
对于详细视图,我建议在模型上实现
get\u absolute\u url
。此方法名称是Django约定。Django管理员将对其进行测试,并链接到它(如果存在)

# models.py
class ProductGroupModel(Model):

    def get_absolute_url(self):
        return reverse('group_detail', args=[self.pk])
然后,您可以轻松地将其用于模型实例:

'Product type <a href="{url}">N/A already exists</a> for this combination.'.format(
    url=obj.get_absolute_url())
“此组合的产品类型”。.format(
url=obj.get\u absolute\u url())