djangocreate-view

CreateView switch model


i have a problem with my CreateView. I wish I could have two different models that change based on the url it finds. I also have problems with success_url, I don't know how to pass it a parameter.

url.py

path('crea-<tipo>', CreaView.as_view(), name="crea")

views.py

class CreaView(StaffMixin, CreateView, tipo):
  if tipo == "gruppo":
    model = Gruppi
  elif tipo == "esercizio":
    model = Esercizio
  fields = '__all__'
  template_name = 'staff/crea.html'
  success_url = '/backoffice/lista/<tipo>'

Solution

  • Juste create two different CreateView:

    GruppiCreateView(StaffMixin, CreateView):
         model = Gruppi
         fields = '__all__'
         template_name = 'staff/crea.html'
         success_url = '/backoffice/lista/gruppi'
    
    EsercizioCreateView(StaffMixin, CreateView):
        model = Esercizio
        fields = '__all__'
        template_name = 'staff/crea.html'
        success_url = '/backoffice/lista/esercizio'