【问题标题】:Using Django templates in an iframe, 404 error在 iframe 中使用 Django 模板,出现 404 错误
【发布时间】:2013-04-24 15:23:49
【问题描述】:

我有一个 Django 站点,主页是 home.html。 home.html 中有一个模式,设置为在主页加载几秒钟后自动打开。模态框内是一个 iframe,应该显示 template1.html。但是,当模态出现时,我有一个 404 并且我被告知该页面与我定义的 url 模式不匹配。我不完全确定在哪里或如何让 template1.html 显示在 iframe 中。用户没有在主页上单击以显示新模板,这就是我熟悉使用 Django 的方式。我是否需要创建另一个 url 模式或视图来实现这一点,或者我是否还有路要走?这是我的文件:

urls.py:

from django.conf.urls.defaults import patterns, url  
from disease import views  

    urlpatterns = patterns('',
    url(r'^$', views.home, name='disease_home'),  
    url(r'^disease/(?P<pk>\d+)/$', 'disease.views.disease', name='disease_disease'),  
    url(r'^treatment/(?P<pk>\d+)$', views.treatment, name='disease_treatment'),

home.html 模态:

<div id="challengeModal" class="reveal-modal xlarge">  
    < iframe seamless scrolling="no" src="challenge1.html" name="challenge1"  
id="challenge1"></iframe>  
< /div>

views.py:

 def home(request):  
    return render(request, "disease/home.html")`  

其他视图根据 template1.html 中的操作返回第二个模板 template2.html

谢谢!

【问题讨论】:

    标签: python django iframe modal-dialog


    【解决方案1】:

    简短的回答是肯定的,您需要另一个 url 模式和 iframe 的视图。 iframe 几乎被视为嵌入式浏览器窗口,它会向服务器发出自己的请求。

    【讨论】:

      【解决方案2】:

      iframe src 只是一个资源的 url。

      例如,您可以使用

      将您的 treatment 页面加载到其中

      &lt;iframe src="/treatment/1/"&gt;&lt;/iframe&gt;

      您将需要创建一个视图来提供您的 challenge1.html 模板和一个用于路由到该视图的 url

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        • 2011-09-29
        • 2018-10-01
        • 2017-09-05
        • 2015-09-06
        相关资源
        最近更新 更多