2016-02-11 45 views
0

我正在開發一個django項目。我有兩個html文件 - sid.htmlpage2.html。首頁是sid.html。點擊一個div,我想加載另一頁page2.html,完全在我的窗口(不是div)。我嘗試了ajax,jquery,但它似乎並沒有工作。我可以知道如何繼續前進,並在視圖,網址中完成這些更改。請幫忙。如何在django項目中加載一個html項目onclick

+0

「它似乎沒有工作。」,而不是哪些呢?什麼不工作?怎麼了?錯誤? – Sayse

+0

你編輯和粘貼什麼是錯誤的,從你的html頁面開始,views.py和urls.py – Transformer

+0

chaitra

回答

0

無法看到您的所有代碼,我們無法做到,但以最基本的方式做到這一點。

網址

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>

希望這會有所幫助,他們將不得不tweeked以適應名你的文件等,但它的起始塊

+0

所以第2頁的html代碼是我與sid相同的文件? – SpeedyH30

0

假設這是你的網址

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

現在你說「的onclick在一個div上,我想加載其他頁面page2.html,完全在我的窗口(不DIV)。我嘗試了阿賈克斯, 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>