简体   繁体   中英

Passing 2 variables through django url

I try to pass 2 variables through url config

url.py

url(r'^essays/(?P<category>[-\w]+)/(?P<slug>[-\w]+)/$', views.EssayDetailView.as_view(), name='essay_view'),

view.py

class EssayDetailView(DetailView):
    model = Essay

    def get_context_data(self, **kwargs):
        context = super(EssayDetailView, self).get_context_data(**kwargs)
        context['category'] = Category.objects.get(slug=self.kwargs['category'])

        return context

but if I use in template url tag like this

<a href = "{{essay.get_absolute_url}}">{{essay.title}}</a>

I've error NoReverseMatch . With 1 parameter it works right. For example url:

url(r'^(?P<slug>[-\w]+)/$', views.ArticleDetailView.as_view(), name='article_view'),

how can i pass 2 variables to generic view? Or should I wright my own view method?

It was mistyping in

def get_absolute_url(self):
        return reverse('articles:essay_view', args=[self.category.slug, self.slug])

now works tnx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM