我收到错误:
'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/
答案 0 :(得分:14)
render()
的第一个参数是request
对象,因此请将您的行更新为
return render(request, 'login.html', c)
它正在尝试引用request.META
,但您传递的是'login.html'
字符串,因此该错误。
答案 1 :(得分:0)
正确的代码是
return render(request,'login.html', c)
例如
from django.shortcuts import render
# Create your views here.
def home_page_view(request):
return render(request,'testapp/home.html')
参考链接:https://docs.djangoproject.com/en/3.0/ref/request-response/