1

I'm trying to add additional data to my template named "equipment_form" linked with my view CreateEquipment (CreateView generic django)

So, my model Equipment possess a subcategory. And my model subcategory possess a category.

For UX reasons, I want my user to chose the category of the equipment first, then the subcategory. In order to do this, I need to get in the view the whole content of the category table and give it to the template. And I have some trouble to figure out how I can do it.

I will really appreciate the help of the community! Thank you.

So atm my view look like this :

class EquipmentCreate(CreateView):
   #category list not passed to the template
   category_list = Category.objects.all()
   model = Equipment
   success_url = reverse_lazy('stock:equipment-list')

EDIT : I found the answer here :

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

Thank you anyway :)

4

1 回答 1

0

在这里找到*答案:

覆盖 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 文档中有关此的更多信息。

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

于 2015-06-16T04:08:32.037 回答