【问题标题】:How to load a html project onclick in django project如何在 django 项目中加载 html 项目 onclick
【发布时间】:2016-05-21 23:31:55
【问题描述】:

我正在开发一个django 项目。我有两个 html 文件 - sid.htmlpage2.html。主页是sid.html。单击 div 时,我想加载另一个页面 page2.html,完全在我的窗口中(不在 div 中)。我尝试了 ajaxjquery,但它似乎不起作用。我可以知道如何继续以及在视图、网址中进行的更改。请帮忙。

【问题讨论】:

  • “它似乎不起作用。”,什么不起作用?什么不工作?发生什么了?错误?
  • 你编辑和粘贴错误的地方,从你的 html 页面开始,views.py 和 urls.py
  • 以上是我的js函数。
  • 'megha' 是 html 页面 page2.html 应该加载的 div onclick 的 id。

标签: html ajax django


【解决方案1】:

假设这是你的网址

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings

urlpatterns = patterns('',
    # Examples:
    #other url
    url(r'^page2/$', 'YourAppName.views.page2'),
)

现在您说“在 div 上单击时,我想加载另一个页面 page2.html,完全在我的窗口中(不在 div 中)。我尝试了 ajax、jquery”。假设这是您要单击的 div <div class="myclass"></div>

function newDoc() {
    window.location.assign("http://www.example.com") // {% url 'yourapps.views.page2' %} the django reverse style
    //always to remember to add 'http' protocol to any url you will use apart from  the django reverse style else you will get Server Error 
}
<div onclick="newDoc()" style="background-color:yellow; height: 20px; cursor:pointer;"></div>

【讨论】:

    【解决方案2】:

    没有看到你所有的代码,我们不能,但最基本的方法就是这样。

    网址

    url(r'^page2/$', 'YourAppName.views.page2'),

    观看次数

    def page2(request): return render(request, 'page2.html')

    在 html 中你可以这样做

    <div class="myclass" href="/page2/">

    或者作为一个js脚本

    <div onclick="location.href='/page2/'">content</div>

    甚至是JQ

    $("div").click(function(){ window.location=$(this).find("a").attr("href"); return false; });

    在你的 HTML 中加入这个

    <div><a href="yourlinkhere">Your link text</a></div>

    希望这会有所帮助,它们必须经过调整以适合您的文件名称等,但这是一个起点

    【讨论】:

    • 那么第 2 页的 html 代码与 sid 是同一个文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2020-03-09
    • 2014-10-03
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多