1.创建django project
2.创建app
在一个project下可以创建多个app,比如运维系统这个project下面包含监控app、cmdb app等等,这些app共享project里的数据。
假如项目创建在/home/django/mysite下,则进入该目录,然后python manage.py startapp cmdb新建了一个app,在/home/django/mysite目录下会出现一个cmdb的目录,里面包含很多.py文件。
3.创建web服务器实现基本请求
修改mysite下的urls.py,
from cmdb import views urlpatterns = [ #url(r\'^admin/\', admin.site.urls), url(r\'^index/\', views.index), ]
修改cmdb下的views.py,
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. # 通过request参数接收用户的页面请求,所以此处是request def index(request): # 不能直接return字符串,需要借助HttpResponse()方法 return HttpResponse(\'123\')
重启django进入浏览器即可查看到效果。
4.导入静态文件(js,css,图片等)、给用户返回html
4.1 返回html
1.在cmdb的同级目录下新建一个templates目录,在templates目录里新建一个index.html,在html里写入内容。 2.修改cmdb的views.py from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): #return HttpResponse(\'123\') return render(request,\'index.html\') 3.修改mysite下的settings.py TEMPLATES = [ { \'BACKEND\': \'django.template.backends.django.DjangoTemplates\', #添加模板目录,让django到DIRS的目录下找对应的html文件。 #BASE_DIR就是settings.py的父目录的父目录。 \'DIRS\': [os.path.join(BASE_DIR,\'templates\')],
4.2 引入静态文件
1.修改settings.py #STATIC_URL就是一个访问前缀。 STATIC_URL = \'/qianzhui/\' STATICFILES_DIRS=(
#最后必须加个“,”,不然会提示不是元组,找不到静态文件 os.path.join(BASE_DIR,\'statics\'), ) 2.修改html <body> <h1>123,test.</h1> <script src="/qianzhui/jquery-1.12.4.js"></script> </body>
5. 实现表单提交
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>123</title> </head> <body> <h1>123,test.</h1> <form action="/index/" method="POST"> <input type="text" name="username" /> <input type="password" name="password" /> <!--input type="email" name="email" /--> <input type="submit" /> </form> <h1>DATA:</h1> <table border="1"> #接收到django传过来的数据data,循环data,将数据在html上展示 {% for item in data %} <tr> <td>{{ item.user }}</td> <td>{{ item.pass }}</td> </tr> {% endfor %} </table> <script src="/qianzhui/jquery-1.12.4.js"></script> <script> <!--alert(\'123\');--> </script> </body> </html>
views.py
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. USER_INPUT=[ {\'user\':\'user1\',\'pass\':\'pass1\'}, {\'user\':\'user2\',\'pass\':\'pass2\'} ] def index(request): if(request.method == "POST"): #如果request.POST.get()里面的参数没有对应值,会报错,所以这里指定了一个None,如果没有就返回None,确保不会报错。 user = request.POST.get(\'username\',None) password = request.POST.get(\'password\',None) temp = {\'user\':user,\'pass\':password} USER_INPUT.append(temp) #return HttpResponse(\'123\') return render(request,\'index.html\',{\'data\':USER_INPUT})
settings.py
MIDDLEWARE = [ \'django.middleware.security.SecurityMiddleware\', \'django.contrib.sessions.middleware.SessionMiddleware\', \'django.middleware.common.CommonMiddleware\', #下面这一行必须暂时注释,不然django会报错,暂时不知道什么意思。 # \'django.middleware.csrf.CsrfViewMiddleware\', \'django.contrib.auth.middleware.AuthenticationMiddleware\', \'django.contrib.messages.middleware.MessageMiddleware\', \'django.middleware.clickjacking.XFrameOptionsMiddleware\', ]
上面html表格里的数据是存储在内存里的,重启页面就会消失,如果想让数据永久保存,就需要将数据写入数据库,往下看。
6. 数据保存到sqlite
cmdb→models.py,
#创建类就等于是建表了;类必须继承models.Model class UserInfo(models.Model): user = models.CharField(max_length=32) passwd = models.CharField(max_length=32)
mysite→settings.py,
INSTALLED_APPS = [ \'django.contrib.admin\', \'django.contrib.auth\', \'django.contrib.contenttypes\', \'django.contrib.sessions\', \'django.contrib.messages\', \'django.contrib.staticfiles\', #注册APP \'cmdb\', ]
创建表,
python manage.py makemigrations
python manage.py migrate
views.py,
from django.shortcuts import render from django.shortcuts import HttpResponse #导入models from cmdb import models # Create your views here. def index(request): if(request.method == "POST"): u = request.POST.get(\'username\',None) p = request.POST.get(\'password\',None) #执行了这行代码,数据库里就有数据了。 models.UserInfo.objects.create(user=u,passwd=p) #return HttpResponse(\'123\') #取出数据 data_list = models.UserInfo.objects.all() return render(request,\'index.html\',{\'data\':data_list})
index.html,
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>123</title> </head> <body> <h1>123,test.</h1> <form action="/index/" method="POST"> <input type="text" name="username" /> <input type="password" name="password" /> <!--input type="email" name="email" /--> <input type="submit" /> </form> <h1>DATA:</h1> <table border="1"> <tr> <th> yonghuming </th> <th> mima </th> </tr> {% for item in data %} <tr> <td>{{ item.user }}</td> <td>{{ item.passwd }}</td> </tr> {% endfor %} </table> <script src="/qianzhui/jquery-1.12.4.js"></script> <script> <!--alert(\'123\');--> </script> </body> </html>
如此,数据就存储到django的默认数据库sqlite里了,重启django或者浏览器,html里的数据依然存在。