有没有办法在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'
答案 0 :(得分:1)
slug在kwargs
中提供,这是一本字典。
context['slug'] = kwargs['slug']