0

How to redirect the user to the desired link after registration? What I do is not working. I want to redirect to "new_channel". Thanks in advance.

views.py

class RegisterFormView(FormView):
form_class = UserCreationForm


success_url = "main/my_newsagent/new_channel.html"


template_name = "main/register.html"

def form_valid(self, form):

    form.save()

    return super(RegisterFormView, self).form_valid(form)

urls.py

urlpatterns = [
path('', views.index, name='index'),
path('main/register/', views.RegisterFormView.as_view(), name='register'),
path('main/login/', views.LoginFormView.as_view(), name='login'),
path('main/logout/', views.LogoutView.as_view(), name='logout'),
path('main/my_news_agent/', views.my_newsagent, name='my_newsagent'),
path('main/my_news_agent/new_channel', views.new_channel, name='new_channel'),
path('main/edit_profile', views.edit_profile, name='edit_profile'),
path('main/my_newsagent_page', views.my_newsagent_page, name='my_newsagent_page'),

path('main/my_newsagent/new_channel.html', views.new_channel, name='new_channel'),

]

  • Does this answer your question? [How can I redirect from view In Django](https://stackoverflow.com/questions/42614172/how-can-i-redirect-from-view-in-django) – manny Mar 27 '20 at 11:25
  • @manny, those are for function-based views, not class-based views. Slightly different but related. – Exelian Mar 27 '20 at 11:26

1 Answers1

0

success_url should not be a file but an url (as the name suggests). You can use the reverse or reverse_lazy functions to generate the appriopriate url:

NB: you might need to change 'new_channel' to the appropriate URL-name.

from django.urls import reverse_lazy

class RegisterFormView(FormView):
    form_class = UserCreationForm
    success_url = reverse_lazy('new_channel')
    template_name = "main/register.html"

    def form_valid(self, form):
        form.save()
        return super(RegisterFormView, self).form_valid(form)
Exelian
  • 5,749
  • 1
  • 30
  • 49
  • I have an error: "django.core.exceptions.ImproperlyConfigured: The included URLconf 'NewsAgent.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probab ly caused by a circular import." :( –  Mar 27 '20 at 11:38
  • If that is the case please import and use the `reverse_lazy` function instead – Exelian Mar 27 '20 at 12:08