Django学习笔记

--Django学习笔记

--------------------2014/10/23

Django的架构:

在django中,urls.py将URL请求转给view.py中的函数,函数将计算后的结果转给templates中的某个xxx.html文件,最后xxx.html文件发给了客户,在客户的页面显示出来。

进来的请求转入/hello/.
Django通过在ROOT_URLCONF配置来决定根URLconf.
Django在URLconf中的所有URL模式中,查找第一个匹配/hello/的条目。
如果找到匹配,将调用相应的视图函数,视图函数返回一个HttpResponse
Django转换HttpResponse为一个适合的HTTP response, 以Web page显示出来

 

在Django中使用bootstrap:

1. 首先在你喜欢的目录创建static文件夹,在文件夹中创建css,img,js,fonts等文件加,将bootstrap的文件放在对应的目录中。

2. 配置参数让Django能找到这些静态文件,在settings.py文件中添加

STATICFILES_DIRS = ('/your/directory/static',)

3. 在html文件中使用静态元素。

<link rel=“stylesheet” href=“/static/css/bootstrap.css” />

4. 在views.py中返回html文件

from django.shortcuts import render_to_response
def index(request):
  return render_to_response('index.html')

这样就可以使用bootstrap了。

#表单

<html>
<head>
    <title>Search</title>
</head>
<body>
    <form action="/search/" method="get">
        <input type="text" name="q">
        <input type="submit" value="Search">
    </form>
</body>
</html>

#表单将属性(name) q的值传入后台

服务器端跳转:

from django.http import HttpResponseRedirect
def ...(request):
    ....
    ....
    return HttpResponseRedirect('/xxxxx/')
原文地址:https://www.cnblogs.com/jackhub/p/4046403.html