Is there a way to add get variables into the url while using permalink?
So www.example.com/1999/news/?filter=entertai开发者_高级运维nment
IMHO Permalink should not contain a query parameter. It doesn't quite sound right.
That said, there is a very crufty and un-Django like way to return an URL like the one you have specified from the get_absolute_url()
method of a model.
Steps
First add a dummy URL and corresponding do-nothing view. For e.g.
# models.py
class MyModel(models.Model):
...
@models.permalink
def get_absolute_url(self):
return ('dummy_url', [str(self.id), self.filter])
# urls.py
url(r'^news/(?P<model_id>\d+)/\?category=(?P<category>\w+)$',
'dummy_url', {}, name = 'dummy_url'),
# views.py
def dummy_url(request, *args, **kwargs):
pass
This dummy will only serve to generate the URL. I.e. you will get the correct URL if you execute instance.get_absolute_url()
.
You will have to add another, proper URL configuration and a matching view to actually display the instance page when the URL is called. Something like this.
# urls.py
url(r'^news/(?P<model_id>\d+)/$',
'correct_view', {}, name = 'correct_view'),
# views.py
def correct_view(request, *args, **kwargs):
# do the required stuff.
The correct_view
will have to extract the GET
parameter from the request
though.
Note how similar the dummy and proper URL configurations are. Only the query parameter is extra in the dummy.
精彩评论