设置:
LOGIN_URL = 'login_view'
意见:
class MyView(LoginRequiredMixin,View):
template_name = "login.html"
login_url = 'login.html'
redirect_field_name = 'redirect_to'
login_view = MyView.as_view()
网址:
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"),
]
模板位于 ./templates/storefront/login.html
我找不到页面(404)
回答1
如果您的模板位于以下目录结构中,则在这里您将两次写入 template_name(第一次在视图中,第二次在 url 中)
templates/storefront/login.html
您只需要在视图文件中描述 template_name 并且您还应该检查您是否在 settings.py 文件中注册了模板文件夹。
回答2
您不会在类视图中创建 Get 或 Post 函数
视图.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
在设置中:您需要输入实际的 URL 或用户lazy_reverse(YOUR_URL_NAME)
LOGIN_URL = lazy_reverse('login_view')
如果用户登录,此视图将起作用,否则会将用户重定向到 login_view
在 urls 中:您在类中编写模板名称,因此您无需再次编写它
path("login_view/",view=MyView.as_view(template_name = "storefront/login.html"), name="login_view"),