Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#17913 closed Uncategorized (invalid)

'X' object has no attribute 'META' in RequestContext

Reported by: Boris Bobrov Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

def v2(request):
    t = Template('')
    c = RequestContext('notarequest')
    return HttpResponse(t.render(c))

returns

AttributeError at /v2/
'str' object has no attribute 'META'

Maybe it should be TypeError with value "the first argument should be HttpRequest object, not str"?

Attachments (1)

requestcontext.diff (924 bytes) - added by Boris Bobrov 5 years ago.

Download all attachments as: .zip

Change History (5)

Changed 5 years ago by Boris Bobrov

Attachment: requestcontext.diff added

comment:1 Changed 5 years ago by Boris Bobrov

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Type: UncategorizedBug

comment:2 Changed 5 years ago by Boris Bobrov

Type: BugUncategorized

Well, now I am not so sure if it is a bug or something else.

comment:3 Changed 5 years ago by Luke Plant

Resolution: invalid
Status: newclosed

There is no Pythonic way to test for a HttpRequest object that fits with duck-typing. In fact, if you set TEMPLATE_CONTEXT_PROCESSORS to an empty list the above code will succeed, and this tolerance could be useful in some testing situations or other situations where you are not using a Django HttpRequest object. So there is nothing RequestContext can do here.

comment:4 Changed 5 years ago by Boris Bobrov

So, the check must be in every context processor?
If TEMPLATE_CONTEXT_PROCESSORS = (), then we get

Exception Type: 	AttributeError
Exception Value: 	'SafeUnicode' object has no attribute 'status_code'
Exception Location: 	django/http/utils.py in conditional_content_removal, line 28

because response == u

my settings.py:

MIDDLEWARE_CLASSES = (
#    'django.middleware.common.CommonMiddleware',
#    'django.contrib.sessions.middleware.SessionMiddleware',
#    'django.middleware.csrf.CsrfViewMiddleware',
#    'django.contrib.auth.middleware.AuthenticationMiddleware',
#    'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = ()
Note: See TracTickets for help on using tickets.
Back to Top