'How would we set up LoginRequiredMixin? I am Getting following Error

Settings:

    LOGIN_URL = 'login_view'

Views:

class MyView(LoginRequiredMixin,View):
    template_name = "login.html"
    login_url = 'login.html'
    redirect_field_name = 'redirect_to'
login_view = MyView.as_view()

Url:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('blogApp.urls')),
    path("login_view/",view=MyView.as_view(template_name = "storefront/login.html"), name="login_view"),
]

And Template is in ./templates/storefront/login.html

I am getting Page not found (404)



Solution 1:[1]

here you are writing template_name two times(first in view and second time in url ) if your template is in the following directory structure

templates/storefront/login.html

you only have to describe the template_name in view file and you should also check if you have registered the template folder in the settings.py file.

Solution 2:[2]

You don't Create Get or Post Function in the Class View

views.py

class MyView(LoginRequiredMixin,View):
    template_name = "template_name.html"
    login_url = lazy_reverse('login_view')
    redirect_field_name = 'redirect_to'
    def get(self, request):
        # do some stuff
    def post(self, request):
        # do some stuff

in Settings : You need to put the actual URL or user lazy_reverse(YOUR_URL_NAME)

LOGIN_URL = lazy_reverse('login_view')

this view will work if the user log in else will redirect the User to login_view

in urls: You write the Template name in the class so You don't need to write it again

path("login_view/",view=MyView.as_view(template_name = "storefront/login.html"), name="login_view"),

Solution 3:[3]

import kotlin.properties.Delegates

object Test {

  private var num by Delegates.notNull<Int>()

  fun test(arr: Array<Int>): Int {
    val min = 1
    val max = arr.maxOrNull() ?: min
    for (i in min..max) {
      if (i !in arr) {
        num = i
        break
      } else if (i == max) {
        num = max + 1
      }
    }
    return num
  }

}

private val arr1 = arrayOf<Int>()
private val arr2 = arrayOf(5, 44, 3, 975, 1, 123, 2, 4)
private val arr3 = arrayOf(1, 4, 3, 2)

println(Test.test(arr1))   // Prints 1
println(Test.test(arr2))   // Prints 6
println(Test.test(arr3))   // Prints 5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vijay Soni
Solution 2 Ayman
Solution 3