2015-10-21 91 views
0

views.pyDjango的網址添加參數

class LaViewSet(viewsets.ModelViewSet): 

    serializer_class = IlSerializer 

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

url.py

url(r'^pippo/(?P<miopar>.+)', views.LaViewSet.as_view({'get': 'list'}), name="Serializzata"), 

這是一個工作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'

回答

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 %} 
1

試試這個:

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