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 :)

Community
  • 1
  • 1
Jerkso
  • 11
  • 4
  • Glad you found the answer -- you may want to paste a small snippet of your solution as the answer, so the question stops showing up on 'unanswered' lists. :) – trpt4him Dec 16 '14 at 21:49
  • I found My answer here : http://stackoverflow.com/questions/9552337/django-createview-how-to-declare-variable-and-use-it-in-templates?rq=1 Finally it's not so hard :) – Jerkso Dec 19 '14 at 14:22

1 Answers1

0

Found* the answer here:

Override get_context_data and set context_data['place_slug'] = your_slug

Something like this:

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

Some more info on this in the Django docs.

*Posting OP's comment and edit as answer

Community
  • 1
  • 1
Ajoy
  • 1,838
  • 3
  • 30
  • 57