Skip to content

Commit dc06b9b

Browse files
committed
add api app
1 parent a45b99a commit dc06b9b

File tree

10 files changed

+60
-0
lines changed

10 files changed

+60
-0
lines changed

api/__init__.py

Whitespace-only changes.

api/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

api/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ApiConfig(AppConfig):
5+
name = 'api'

api/migrations/__init__.py

Whitespace-only changes.

api/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

api/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

api/urls.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.urls import path
2+
from api import views
3+
4+
urlpatterns = [
5+
# learning interface test:
6+
# ex : /v1/hello_world/
7+
path('hello_world/', views.hello_world),
8+
path('user/<slug:username>', views.user),
9+
path('get_user_info/<int:uid>', views.get_user_info),
10+
11+
]

api/views.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.shortcuts import render
2+
from django.http import JsonResponse
3+
4+
5+
def hello_world(request):
6+
"""
7+
最简单的get请求,返回json格式返回
8+
"""
9+
return JsonResponse({"code": 10200, "message": "Welcome to API testing"})
10+
11+
12+
def user(request, username):
13+
"""
14+
通过 URI 传参
15+
"""
16+
msg = "hello, {}".format(username)
17+
return JsonResponse({"code": 10200, "message": msg})
18+
19+
20+
def get_user_info(request, uid):
21+
"""
22+
根据用户id返回数据
23+
"""
24+
if request.method == "GET":
25+
user_info = {"id": 1, "name": "tom", "age": 22}
26+
if uid != 1:
27+
response = {"code": 10101, "message": "user id null"}
28+
else:
29+
response = {"code": 10200, "message": "success", "data": user_info}
30+
return JsonResponse(response)
31+
else:
32+
return JsonResponse({"code": 10101, "message": "request method error"})

guest2/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'django.contrib.staticfiles',
4040
'sign',
4141
'bootstrap3',
42+
'api',
4243
]
4344

4445
MIDDLEWARE = [

guest2/urls.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
path('logout/', views.logout),
3838
path('api/', include('sign.urls')),
3939

40+
path('v1/', include('api.urls')),
41+
4042
]
4143

4244

0 commit comments

Comments
 (0)