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

时间:2021-01-31 19:34:45

I am getting the error:

我收到错误:

'str' object has no attribute 'META'

The Traceback highlights this bit of code:

Traceback突出显示了这段代码:

return render('login.html', c)

Where that bit of code is in my views.py:

那些代码在我的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)

This is what my template looks like:

这就是我的模板:

{% 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 %}  

I assume I might be using render() incorrectly but in the docs I think I am putting in the correct parameters.

我假设我可能正在使用render(),但在文档中我认为我正在使用正确的参数。

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

1 个解决方案

#1


14  

First parameter to render() is request object, so update your line to

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

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

It is trying to refer request.META, but you are passing 'login.html' string, hence that error.

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

#1


14  

First parameter to render() is request object, so update your line to

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

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

It is trying to refer request.META, but you are passing 'login.html' string, hence that error.

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