Django登录函数 - TypeError:context必须是dict而不是Context

时间:2017-07-21 13:24:52

标签: python django dictionary django-csrf

我正在研究Django项目。我的登录视图有问题。无论我尝试什么,我都会遇到类型错误。

以下是html模板和登录视图:

的login.html

{% extends "base.html" %}
{% load bootstrap_tags %}
{% block content %}
    <form role="form" method="post" action="{% url 'login' %}">
        <legend>Login</legend>
        {% csrf_token %}
        {{ form| as_bootstrap }}
        <div class="form-group">
            <button type="submit" class="btn btn-primary">Login</button>
        </div>
    </form>
{% endblock %}

帐户/ views.py

def login(request):
    if request.method == 'POST':
        form = UserLoginForm(request.POST)
        if form.is_valid():
            user = auth.authenticate(email=request.POST.get('email'),
                                     password=request.POST.get('password'))

            if user is not None:
                auth.login(request, user)
                messages.error(request, "You have successfully logged in")
                return redirect(reverse('index'))
            else:
                form.add_error(None, "Your email or password was not recognised")

        else:
            form = UserLoginForm()

    args = {'form': form}
    args.update(csrf(request))
    return render(request, 'login.html', args)

当我尝试从本地服务器上的索引导航到登录模板时,它会返回错误:

  

typeError:context必须是dict而不是Context。

有人请说明这个错误,但还没有找到解决方案。

已更新:完整追溯

Traceback:

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/movie_app/MovieApp_prj/accounts/views.py" in login
  117.     return render(request, 'login.html', args)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  68.     return template.render(context, request)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render
  207.                     return self._render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in render
  1040.             output = self.filter_expression.resolve(context)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/base.py" in resolve
  736.                 new_obj = func(obj, *arg_vals)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django_forms_bootstrap/templatetags/bootstrap_tags.py" in as_bootstrap
  28.     return template.render(c)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/backends/django.py" in render
  64.         context = make_context(context, request, autoescape=self.backend.engine.autoescape)

File "/Users/Andrew/Documents/Code/code-institute-projects/stream-three/env/lib/python2.7/site-packages/django/template/context.py" in make_context
  287.         raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)

Exception Type: TypeError at /login/
Exception Value: context must be a dict rather than Context.

0 个答案:

没有答案