Django2.0,python-version3.6,django-simpl-captcha-5.6,为什么我的验证码没有出现?

Django2.0,python-version3.6,django-simpl-captcha-5.6,Why is my verification code not appearing?

我尝试为用户 login.I 练习一个 html 页面浏览文档并按照步骤尝试 django-simple-captcha 来验证我的用户 login.finally... enter image description here

我已经makemigrations, migrate and add captcha to INSTALL_APPS,定义了Form,定义了视图函数

'views.py'
from django.shortcuts import render
from django.views.generic.base import View

from .froms import UserLoginForm
# Create your views here.
class LoginView(View):
    def get(self, request):
    user_login_form = UserLoginForm()
    return render(request, 'login.html', {'user_login_form':user_login_form})


'forms.py'
 from django import forms
 from captcha.fields import CaptchaField

class UserLoginForm(forms.Form):
    username = forms.CharField(required=True)
    password = forms.CharField(required=True, min_length=5)
    captcha = CaptchaField()

'urls.py'
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', TemplateView.as_view(template_name='login.html')),
    path('captcha/', include('captcha.urls')),
]

<html>
 <head>
  <title>login</title>
 </head>
 <body>
  <form action="#" method="post">
   <p>
    Username:<input type="text" name="username" placeholder="Please input you email">
   </p>
   <p>
    Password:<input type="password" name="password">
   </p>
   <p>
    <label>code:</label>
       {{ user_login_form.captcha }}
            </p>
   <input type="submit" value="submit">
  </form>
 </body>
</html>

问题是您只是在 urls.py 中使用 TemplateView 呈现页面。这就是它不将 user_login_form 传递给模板上下文的原因。您应该在 urls.py.

中使用 LoginView

this example