Django url在模板中添加param的正确方法

2024-09-30 10:38:04 发布

您现在位置:Python中文网/ 问答频道 /正文

视图.py

class LaViewSet(viewsets.ModelViewSet):

    serializer_class = IlSerializer

    def get_queryset(self):
        ilfiltro = self.kwargs['miopar']
        return models.Pippo.objects.filter(pippo=ilfiltro)

网址.py

^{pr2}$

这是一个有效的url:

http://127.0.0.1:8000/pippo/1

但如果我放一个模板:

{% url '1' 'Serializzata' %};

或者

{% url 'Serializzata'?1 %};

继续出现此错误:

TemplateSyntaxError: Could not parse the remainder: '?1' from ''Serializzata'?1'


Tags: pyself视图urlgetdefclassserializer
2条回答

docs

url

Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. Any special characters in the resulting path will be encoded using iri_to_uri().

This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates:

{% url 'some-url-name' v1 v2 %}

所以在你的情况下:

{% url 'Serializzata' 1 %}

试试这个:

<a href="{% url 'Serializzata' 1 %}">

相关问题 更多 >

    热门问题