0

I am developing a web application (for learning purposes) and I am stuck at how to load another page after a particular button is clicked

I am using django 1.4.5 web framework along with python 2.7.4 I have already designed all the html pages and javascript and css files.

I have no idea about HttpRequests. And studying various tutorials i came across these three methods.

1)

xhr = new XMLHttpRequest();
xhr.open("GET","screen2/",false);
xhr.send(null);

2)

location.href="/screen2/"

3)

window.open("/screen2/","_parent");

all of 'em are kept inside the function setup(),the button's onclick is set to onclick="setup()"

Yes, I comment them out to keep only one of them "alive" at a time.

django urls.py file:

urlpatterns = patterns('',
    url(r'^screen1/$',screen1),
    url(r'^screen2/$',screen2),
)

django views.py file

def screen1(request):
    f = open(r"mysite/frontend/1st screen.html",'r')
    html = f.read()
    return HttpResponse(html)

def screen2(request):
    f = open(r'mysite/frontend/2nd Screen.html','r')
    html = f.read()
    return HttpResponse(html)

Second and third methods work the first doesn't. I don't understand why.

Also, How to send the corresponding scripts and css files with the request ?

4

1 回答 1

1

第一个是使用 javacript 发出 ajax 请求。响应包含在 javascript 中。其他 2 个正在更改网页的位置 (url)。

另外:Django 提供了一个完全包含的模板引擎,以及为您包装所有模板加载的模板加载器。您不必手动打开文件并读取其内容并将其作为字符串返回,如下所示:

def screen2(request):
    f = open(r'mysite/frontend/2nd Screen.html','r')
    html = f.read()
    return HttpResponse(html)

django教程提供了如何使用 djangos 内置函数呈现模板的示例

我假设您没有进行任何配置来提供静态文件(如何随请求发送相应的脚本和 css 文件?)。Django在此处提供了如何执行此操作的示例

于 2013-05-10T20:02:07.900 回答