1.启动django服务
python manage.py runserver 0.0.0.0:8000
2.Django中各文件的作用
1.创建项目
# django-admin.py startproject Djangoproject
2.创建APP
# django-admin.py startapp TestModel(or:python manage.py startapp app_name)
*IF the last command can't create app successfully,you can try the next one.*
# django-admin startapp TestModel
创建完成后的目录结构为(template文件夹为自己创建):
1.在Djangoproject下新建python文件testdbget.py
# -*- coding: utf-8 -*-
from django.http import HttpResponse
def add(request,a,b):
c = int(a) + int(b)
return HttpResponse(str(c))
2.配置到urls文件下
from .import testdbget
urlpatterns = [
url(r'^add/(\d+)/(\d+)/$', testdbget.add, name='add'),
]
3.可以访问:“http://127.0.0.1:8000/add/345/500/ ”即可输出结果
4.urls.py文件配置访问地址+ python文件和方法
url(r'^add/(\d+)/(\d+)/$', testdbget.add, name='add'),
url(r'^$', TestModel_view.index, name='dbget'),
i)python 文件可以配置模版view.py
def index(request):
return render(request,'dbget.html')
ii)**name**可以写在python和html文件中 直接访问
<!DOCTYPE html>
<html>
<head>
<title>add</title>
</head>
<body>
<a href="/add/4/5/">4+5</a>
</body>
</html>
3.模型配置
1.进入数据库
# python manage.py shell
2.导入models
# from TestModel.models import Test
3.查询
# Test.objects.all()