1、pip install django==2.2.0
一、
django-admin startproject UITEST
python manage.py startapp paltform
1. 创建方式:
# 方式 1:终端输入
django-admin startproject UITEST
# 方式 2:
pycharm 中新建 django 项目
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'paltform'
]
class myclass(models.Model):
aa = models.CharField(max_length=111)
bb = models.CharField(max_length=111)
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
127.0.0.1:8000/admin
```
from django.conf.urls import url
from django.contrib import admin
import views
urlpatterns = [
url(r'admin/', admin.site.urls),
url(r'$', views.login_view),
]
```
#coding=utf-8
from django.http.response import HttpResponse
def login_view(request):
return HttpResponse('hello world')
from django.http import JsonResponse
def getproject(request): #
reportid = request.GET.get("aa")
print(reportid)
back = {
'code': 200,
'message': '执行完成'
}
return JsonResponse(back)
def postproject(request):
data = json.loads(request.body.decode())
print(request.body)
print(type(data))
print(data)
add = myclass(aa='wahaha', bb='hahawa')
add.save() # 不 save 无法保存到数据库
myclass.objects.create(aa='wahaha1', bb='hahawa')
back = {
'code': 200,
'message': '执行完成'
}
return JsonResponse(back)
1、STATICFILES_DIRS = [
os.path.join(BASE_DIR,"templates")
]
def getreport(request):
return render(request, '2020-03-31_11_04_43_result.html')
以上就是 django 的基础知识,用 django 写接口原来如此简单,有疑问的可以加我微信,有视频讲解这节课!liuxiaolu4986