python - 'str' 对象没有属性 'META'

标签 python django

我收到错误:

'str' object has no attribute 'META'

Traceback 突出显示了这段代码:

return render('login.html', c)

那段代码在我的 views.py 中的位置:

from django.shortcuts import render
from django.http import HttpResponseRedirect    # allows us to redirect the browser to a difference URL
from django.contrib import auth                 # checks username and password handles login and log outs
from django.core.context_processors import csrf # csrf - cross site request forgery. 

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

这是我的模板的样子:

{% extends "base.html"%}

{% block content %}

    {% if form.errors %}
        <p class = 'error'>Sorry, that's not a valid username or password</p>
    {% endif %}

    <form action = '/accounts/auth/' method = 'post'> {% csrf_token %}
        <label for = 'username'>User name: </label>
        <input type = 'text' name = 'username' value = '' id = 'username'>
        <label for = 'password'>Password: </label>
        <input type = 'password' name = 'password' value = '' id = 'password'>

        <input type = 'submit' value = 'login'>
    </form>
{% endblock %}  

我假设我可能错误地使用了 render() 但在文档中我认为我输入了正确的参数。

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

最佳答案

render() 的第一个参数是request 对象,因此将您的行更新为

return render(request, 'login.html', c)

它试图引用 request.META,但您传递的是 'login.html' 字符串,因此出现错误。

关于python - 'str' 对象没有属性 'META',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20045175/

相关文章:

python - P4API.cpp :39:20: fatal error: Python. h: 没有那个文件或目录

javascript - 使用静态模板标签在 Django 中加载 Javascript

python - 使用 strftime 在 Jinja2(Flask) 中格式化 "posted x seconds ago"?

python - 对数组中每个元素直到当前元素进行条件迭代

python - Django 项目的 Heroku 日志丢失错误

python - 动态模板标签

Python开发-意想不到的缩进问题

Django Activity Feed(Feedly 集成?)

python - 使用 PIL 在图像上添加文本

python - 在类实例列表中的列表属性中查找匹配项