Search code examples
djangotemplatesurlauthenticationrequired

Login Required Django Classed Based View


Totally beginner question.

I have this view in my app Foo:

class PaymentsView(LoginRequiredMixin, CreateView):

    login_url = '/login/'
    redirect_field_name = 'redirect_to'

    # -- Omitted ...

I have read the django documentation but I didn't see where to put my file "login.html" and how can I set the url for that. When I run the code show the 404 error.


Solution

  • Setting login_url will redirect to a URL within your project, which is then handled by URL Dispatcher. You'll need to include a path to that URL within your URLConf, just like you would with any other URL:

    urls.py:

    from django.urls import path
    
    from . import views
    
    urlpatterns = [
        path('login/', views.login),
    ]
    

    You'll then need to write a view to handle the login. Django provides pre-written views and URL patterns that handle this with its login system.