【问题标题】:Django Generic Createview : Add another object to templateDjango Generic Createview:向模板添加另一个对象
【发布时间】:2015-02-15 02:05:13
【问题描述】:

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

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

出于用户体验的原因,我希望我的用户先选择设备类别,然后再选择子类别。为了做到这一点,我需要在视图中获取 类别表并将其提供给模板。而且我很难弄清楚我该怎么做。

我将非常感谢社区的帮助!谢谢。

所以 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

还是谢谢你:)

【问题讨论】:

  • 很高兴您找到了答案——您可能想要粘贴您的解决方案的一个小 sn-p 作为答案,这样问题就不会出现在“未回答”列表中。 :)
  • 我在这里找到了我的答案:stackoverflow.com/questions/9552337/… 终于没那么难了 :)

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


【解决方案1】:

找到*答案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 的评论并编辑为答案

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 2013-10-20
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多