使用从同一视图创建的参数的 CreateView 重定向

CreateView redirect with parameter created from the same view

我有一个为我创建对象的 CreateView,在创建它之后,我希望它通过它的 'nome_scheda'(一个文本字段)在该对象中呈现我。

views.py

class SchedaCreateView(CreateView):
  model = Schede
  fields = ['nome_scheda','data_inizio','data_fine']
  template_name = 'crea/passo1.html'

  def form_valid(self, form):
    form.instance.utente = self.request.user
    return super().form_valid(form)

urls.py

path('crea/<nome>/', creazione, name="creazione_passo1"),

您可以覆盖 .get_success_url(…) method 到 return URL 以由浏览器重定向到:

class SchedaCreateView(CreateView):
    model = Schede
    fields = ['nome_scheda','data_inizio','data_fine']
    template_name = 'crea/passo1.html'

    def form_valid(self, form):
        form.instance.utente = self.request.user
        return super().form_valid(form)

    def <strong>get_success_url</strong>(self):
        return reverse('creazione_passo1', kwargs={ 'nome': <strong>self.object.nome</strong> })