Tpircsnart
Tpircsnart

Reputation: 47

How to redirect url for CreateView in views.py

  1. class A_modelCreateView(CreateView)in views.py
  2. path('create/'),view.A_modelCreateView().as_view(),name='create' in urls.py
  3. build an HTML as form for A_model form but not in its default directory(my_app/A_model.form)

Is there filled parameter somewhere for redirect the url you want?

Upvotes: 2

Views: 352

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

Yes, a CreateView has as one of its baseclasses a the FormMixin. This FormMixin class has a success_url attribute [Django-doc].

You can add a real url, or work with reverse_lazy [Django-doc] to calculate the URL based on the name of a view. For example:

from django.urls import reverse_lazy
from django.views.generic.edit import CreateView

class A_modelCreateView(CreateView):
    success_url = reverse_lazy('overview_page')
    # ...

where overview_page is here the name of a hypothetical view.

If you do not specify a success_url in a CreateView or UpdateView, it will take the get_absolute_url of the model object that is created/updated, if the model has such method. For more information, see the Django documentation.

Upvotes: 3

Related Questions