我在 Django 中使用 LoginView 類。我們可以使用此類作為帶有 HTML 模板的登錄視圖,如下所示:from django.contrib.auth.views import LoginViewurlpatterns = [path('', LoginView.as_view(), {'template_name': 'login.html'}),]登錄.html:<head> <title>test<title> {% load static %}</head><body> <form method="POST"> {% csrf_token %} {{ form.as_p }} </form></body>我的問題是在哪里呈現(xiàn)函數(shù)字典或 httprequest 填充?我如何改變,form.as_p以login_form.as_p,例如?
2 回答

繁星淼淼
TA貢獻1775條經驗 獲得超11個贊
您必須像這樣使用自定義登錄視圖:
from django.contrib.auth.views import LoginView
class CustomLoginView(LoginView):
def get_context_data(self, **kwargs):
"""Insert the form into the context dict."""
if 'login_form' not in kwargs:
kwargs['login_form'] = self.get_form()
# Another your context
return super().get_context_data(**kwargs)
添加回答
舉報
0/150
提交
取消