首页 > 解决方案 > I am getting AttributeError in RequestContext in DJango

问题描述

I wanted Pass URL parameter in iframe issue as mentioned Passing URL parameter in iframe issue

I tried using following https://glitch.com/edit/#!/tf-embed-with-params?path=README.md:1:0

Traceback:

Exception Type: AttributeError
Exception Value:    
'RequestContext' object has no attribute 'META'

views.py

from django.shortcuts import render
from django.template import RequestContext

def survey(request):
    return render(RequestContext(request),'wfhApp/survey.html')

And my html page is as follow:

<!DOCTYPE html>
{% load django_typeform %}
{% load sekizai_tags %}
<html>
  <head>
    <title>Hello!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/styles.css">

  </head>
  <body>

    <h1>Hi there!</h1>
    <div class="target-dom-node" style="width: 100%; height: 500px;"></div>
    <script src="https://embed.typeform.com/embed.js"></script>

    <script src="/survey/script.js"></script>
    {% typeforms_embed 'https://theother2thirds.typeform.com/to/hNZW30' 'New typeform' '{"hideHeaders": true, "hideFooter": true}' %}

    </body>
</html>

urls.py

from django.conf.urls import url
from wfhApp import views

app_name = 'wfhApp'

urlpatterns = [
     url(r'^survey/$',views.survey, name='survey'),
]

标签: pythondjangopython-3.x

解决方案


问题是您正在包装对象的request内部,这对于functionRequestContext是不正确render()

render()函数将为您构建RequestContext对象,因此它期望request和任何额外的上下文变量作为参数。

相反,只需将请求直接传递给render()函数:

def survey(request):
    return render(request, 'wfhApp/survey.html')

推荐阅读