Django使用CreateView添加Slug

时间:2014-03-29 20:36:25

标签: python django

有没有办法在Django CreateView中添加一个slug,我可以在我的上下文中使用该页面。例如,我试过这个:

url(r'^registration/profile/(?P<slug>[\w\-\_]+)/?$', RegistrationView.as_view(),
                           name='registration_profile'),

视图

class RegistrationView(BaseCreateView):
    form_class = AppUserIntroducerCreateForm
    template_name = "registration/register.html"
    slug_url_kwarg = 'slug'


    def get_context_data(self, **kwargs):
        context = super(RegistrationView, self).get_context_data(**kwargs)
        context['slug'] = self.slug.slug
        return context

但仍然获得AttributeError: 'RegistrationView' object has no attribute 'slug'

1 个答案:

答案 0 :(得分:1)

slug在kwargs中提供,这是一本字典。

context['slug'] = kwargs['slug']