Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Generic Createview:向模板添加另一个对象_Django_Forms_Subclassing_Django Generic Views_Createobject - Fatal编程技术网

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

Django Generic Createview:向模板添加另一个对象,django,forms,subclassing,django-generic-views,createobject,Django,Forms,Subclassing,Django Generic Views,Createobject,我正在尝试向名为equipment_表单的模板添加其他数据,该表单与我的视图CreateEquipment CreateView generic django链接 因此,我的模型设备有一个子类别。我的模型子类别拥有一个类别 出于用户体验的原因,我希望用户首先选择设备的类别,然后选择子类别。为了做到这一点,我需要在视图中看到 类别表并将其交给模板。我很难想出我该怎么做 我将非常感谢社区的帮助!多谢各位 所以我的观点是这样的: class EquipmentCreate(CreateView):

我正在尝试向名为equipment_表单的模板添加其他数据,该表单与我的视图CreateEquipment CreateView generic django链接

因此,我的模型设备有一个子类别。我的模型子类别拥有一个类别

出于用户体验的原因,我希望用户首先选择设备的类别,然后选择子类别。为了做到这一点,我需要在视图中看到 类别表并将其交给模板。我很难想出我该怎么做

我将非常感谢社区的帮助!多谢各位

所以我的观点是这样的:

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

无论如何,谢谢你:

找到了*答案:

覆盖获取上下文数据并设置上下文数据['place\u slug']=您的上下文数据

大概是这样的:

def get_context_data(self, **kwargs):
    context = super(PictureCreateView, self).get_context_data(**kwargs)
    context['place_slug'] = self.place.slug
    return context
更多关于这方面的信息,请访问


*张贴OP的评论并编辑为答案

很高兴您找到了答案-您可能希望粘贴一小段解决方案作为答案,这样问题就不会出现在“未回答”列表中:我在这里找到了答案:终于没那么难了: