The str object does not have the META attribute

I get an error message:

'str' object has no attribute 'META'

Traceback highlights this bit of code:

return render('login.html', c)

Where is this bit of code located in my view.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)

Here's what my template looks like:

{% extends "base.html"%}

{% block content %}

    {% if form.errors %}
        <p class = 'error'>Sorry, that 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 suppose that I might be misused render(), but in the docs I think I enter the correct parameters.

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

+4
source share
1 answer

The first parameter render()is the object request, so update your line to

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

It tries to pass request.META, but you pass the string 'login.html', therefore this error.

+11
source

All Articles