If I have a model like this:
class Article(models.Model):
title = models.CharField(max_length=200)
# ... rest of the code ...
def get_absolute_url(self):
return reverse('article-detail', args=[str(self.pk)])
and I have an url mapping like this:
url(r'^article/(?P<pk>[0-9]+)/$', views.ArticleView.as_view(), name='article-detail'),
In template should I use:
<a href="{{ article.get_absolute_url }}">{{ article.title }}</a>
or
<a href="{% url 'article-detail' article.pk %}">{{ article.title }}</a>
I'm still thinking both are good ideas, but which is the best?
In the first code I've written args=[str(self.pk)]
, why I must convert self.pk into string? URLs must be strings?
In my generic view, how do I use pk variable?
I'm really confused with that slug_field
, slug_url_kwarg
, pk_url_kwarg
, query_pk_and_slug
.
Which matches which?
If I set query_pk_and_slug
to True, slug_field
= pk?