Reputation: 16469
I am getting the error:
'str' object has no attribute 'META'
The Traceback highlights this bit of code:
return render('login.html', c)
Where that bit of code is in my 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.
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/
Upvotes: 9
Views: 26408
Reputation: 1
I got the same exact error message. And this is how I fixed that
def login(request):
if request.method =='POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, username, password) #this is wrong
messages.success(request,"You are now logged in")
return redirect('dashboard')
else:
messages.error(request, "Your credentials invalid")
return redirect('login')
return render(request, 'accounts/login.html')
def login(request):
if request.method =='POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user) # this is correct
messages.success(request,"You are now logged in")
return redirect('dashboard')
else:
messages.error(request, "Your credentials invalid")
return redirect('login')
return render(request, 'accounts/login.html')
Notice that I made a mistake there by adding username and password on the first code, and I fixed that on the second code
Upvotes: 0
Reputation: 564
Although this mistake is very uncommon, yet I made it. Hence I am stating it here so that noone else makes that mistake. I passed my request parameter as a String.
return render('request', 'login.html', c)
#Wrong
What I should have done instead is that I should have removed the quotes around request.
return render(request, 'login.html', c)
#Correct
This solved my problem.
Upvotes: 0
Reputation: 1
I got the same error below:
'str' object has no attribute 'META'
Because I used @stringfilter for test()
and rendered index.html
in it as shown below:
# "views.py"
from django.shortcuts import render
from django.template.defaultfilters import stringfilter
@stringfilter # Here
def test(request):
return render(request, 'index.html')
So, I removed @stringfilter
from test()
as shown below, then the error was solved:
# "views.py"
from django.shortcuts import render
from django.template.defaultfilters import stringfilter
# @stringfilter # Here
def test(request):
return render(request, 'index.html')
Upvotes: 0
Reputation: 1
def get(self, request,pk, *args, **kwargs):
emp=Emp.objects.get(id=pk)
#here after json if we take like these without 'single quotes it wont rise error
emp_data=serialize('json',[emp],fields=('ename','esal','eadres'))
Upvotes: -1
Reputation: 301
Correct code is
return render(request,'login.html', c)
for example
from django.shortcuts import render
# Create your views here.
def home_page_view(request):
return render(request,'testapp/home.html')
Reference link:https://docs.djangoproject.com/en/3.0/ref/request-response/
Upvotes: 0
Reputation: 53316
First parameter to render()
is request
object, so update your line to
return render(request, 'login.html', c)
It is trying to refer request.META
, but you are passing 'login.html'
string, hence that error.
Upvotes: 19