python基础问题

小A软件测试 2017-11-01 07:47:48
在使用django框架开发web的过程中,我需要使用django-simple-captcha来使用验证码功能,python3.5,django1.11.6,captcha0.4.6,但是在按照官方的帮助写代码的时候,却出现了这个问题,由于不知道其中的原理,所以纠结了很久,百度也查不到解决方案,只好来求助、错误代码:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\PyProject\TestCp\MyTest\views.py", line 16, in some_view
return render_to_response('register.html', locals()) # 如果为真,则进入指定页面
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\shortcuts.py", line 21, in render_to_response
content = loader.render_to_string(template_name, context, using=using)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\loader.py", line 68, in render_to_string
return template.render(context, request)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 1046, in render
return render_value_in_context(output, context)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\template\base.py", line 1024, in render_value_in_context
value = force_text(value)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\utils\encoding.py", line 76, in force_text
s = six.text_type(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\utils\html.py", line 385, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\forms\boundfield.py", line 41, in __str__
return self.as_widget()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\forms\boundfield.py", line 127, in as_widget
**kwargs
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\captcha\fields.py", line 101, in render
return super(CaptchaTextInput, self).render(name, self._value, attrs=attrs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\forms\widgets.py", line 220, in render
context = self.get_context(name, value, attrs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\forms\widgets.py", line 792, in get_context
context = super(MultiWidget, self).get_context(name, value, attrs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\django-1.11.6-py3.5.egg\django\forms\widgets.py", line 211, in get_context
'attrs': self.build_attrs(self.attrs, attrs),
TypeError: build_attrs() takes from 1 to 2 positional arguments but 3 were given


根据我调试的结果,view.py中是可以接受到验证码的,但是在传到模版去显示的是时候除了问题,传不过去。
view.py
from django.shortcuts import render,render_to_response
from .form import RegisterForm


def some_view(request):
if request.POST:
form = RegisterForm(request.POST)

# Validate the form: the captcha field will automatically
# check the input
if form.is_valid():
human = True
else:
form = RegisterForm()

return render_to_response('register.html', locals()) # 如果为真,则进入指定页面
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ form.captcha }}
</body>
</html>
...全文
333 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhaoHank 2017-12-31
  • 打赏
  • 举报
回复
我也遇到过,升级下captcha就可以了
newdas123 2017-11-17
  • 打赏
  • 举报
回复
我也碰到这个问题,不知道怎么解决

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧