'str' object has no attribute 'META'
Asked Answered
C

6

9

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/

Calculus answered 18/11, 2013 at 10:12 Comment(0)
C
19

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.

Contemporize answered 18/11, 2013 at 10:15 Comment(2)
ugh how did i not see this...it's getting late. Thank you!!Calculus
Thanks. In my case I put quotes around request, duh! return render('request', "gameplay/game_detail.html", {'game': game})Instead
Z
0

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/

Zarf answered 6/4, 2020 at 13:12 Comment(0)
M
0

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')
Mincemeat answered 26/6, 2023 at 12:56 Comment(0)
T
0

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.

Tomekatomes answered 14/8, 2023 at 13:24 Comment(0)
R
0

I got the same exact error message. And this is how I fixed that

before

views.py

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')

after

views.py

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

Respectively answered 28/11, 2023 at 10:17 Comment(1)
This is pretty hard to read... we need to mentally diff two blocks of code that aren't even lined up beside each other. Please consider showing just one code block, e.g. maybe a correct one with the incorrect line commented out and clearly marked.Interpol
G
-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'))
Garmon answered 15/8, 2021 at 16:23 Comment(1)
How does this answer the question?Shelves

© 2022 - 2024 — McMap. All rights reserved.