Django Generic Createview:向模板添加另一个对象

时间:2014-12-16 17:09:53

标签: django forms subclassing django-generic-views createobject

我尝试将其他数据添加到名为" equipment_form"的模板中。链接到我的视图CreateEquipment(CreateView generic django)

所以,我的模型设备拥有一个子类别。我的模型子类别拥有一个类别。

出于用户体验的原因,我希望我的用户首先选择设备的类别,然后选择子类别。为了做到这一点,我需要在视图中查看整个内容 类别表并将其提供给模板。我有点麻烦弄清楚我是怎么做到的。

我真的很感谢社区的帮助!谢谢。

所以atm我的观点看起来像这样:

class EquipmentCreate(CreateView):
   #category list not passed to the template
   category_list = Category.objects.all()
   model = Equipment
   success_url = reverse_lazy('stock:equipment-list')
编辑:我在这里找到了答案:

Django - CreateView - How to declare variable and use it in templates

无论如何,谢谢你。)

1 个答案:

答案 0 :(得分:0)

找到 * 答案here

  

覆盖get_context_data并设置context_data ['place_slug'] = your_slug

     

这样的事情:

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

Django docs中的更多相关信息。

*发布OP的评论并编辑为答案