pip install django
pip install djangorestframework
python manage.py startapp api
from django.contrib import admin
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include('api.urls')),
]
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class LoginView(APIView):
def post(self, request, *args, **kwargs):
print(request.data)
return Response({
"status": True})
from django.contrib import admin
from django.conf.urls import url, include
from api import views
urlpatterns = [
url(r'^login/',views.LoginView.as_view()),
]
修改djangoProject20211114裡面的settings.py中的 INSTALLED_APPS,在末尾添加一句 ‘rest_framework’
參考鏈接:https://www.bilibili.com/video/BV1i64y1y7mm?p=25
python manage.py migrate
創建完會有以下界面
python manage.py createsuperuser
127.0.0.1:8000/admin
在api文件夾下的models.py文件中添加以下內容
from django.db import models
# Create your models here.
class ImageInfo(models.Model):
url_height = models.PositiveIntegerField(default=150)
url_width = models.PositiveIntegerField(default=150)
admin_icon = models.ImageField(upload_to="imgs/", height_field='url_height', width_field='url_width')
添加如下一句在djangoProject20211114下的settings.py的 INSTALLED_APPS
‘api.apps.ApiConfig’,
(api是你創建的功能名稱)
/根目錄/settings.py
在添加INSTALLED_APPS = [
功能名稱.apps.功能名稱Config
]
python manage.py makemigrations api
python manage.py makemigrations 功能名稱
pip install Pillow
python manage.py migrate
添加如下一句在djangoProject20211114下的admin.py
from django.contrib import admin
# Register your models here.
from .models import ImageInfo
class ImageInfoAdmin(admin.ModelAdmin):
list_display = ["url_height", "url_width", "admin_icon"]
admin.site.register(ImageInfo, ImageInfoAdmin)
解決方案:
在djangoProject20211114下的settings.py中,文件末尾添加
MEDIA_URL = "/upimg/"
MEDIA_ROOT = os.path.join(BASE_DIR, "upimg")
upimg這個名字可以自己指定
在djangoProject20211114下的urls.py中引入配置
from djangoProject20211114.settings import MEDIA_ROOT
from django.views.static import serve
url(r'^upimg/(?P<path>.*)$', serve, {
"document_root": MEDIA_ROOT})
參考鏈接:https://www.bilibili.com/video/BV1oU4y1w7PF?spm_id_from=333.999.0.0
pythons zhconv library provide
introduction : Refer to some o