我在DJango的RequestContext中获得AttributeError

2024-03-28 08:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在iframe问题中传递URL参数,如前所述Passing URL parameter in iframe issue

我尝试使用以下https://glitch.com/edit/#!/tf-embed-with-params?path=README.md:1:0

回溯:

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

我的html页面如下所示:

<!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>

url.py

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

app_name = 'wfhApp'

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

Tags: djangonamefromhttpsimportcomhtmlscript
1条回答
网友
1楼 · 发布于 2024-03-28 08:29:51

问题是您正在将request包装在RequestContext对象中,这对于the ^{} function是不正确的

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

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

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

相关问题 更多 >