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()),
]
modify djangoProject20211114 Inside settings.py Medium INSTALLED_APPS, Add a sentence at the end ‘rest_framework’
Reference link :https://www.bilibili.com/video/BV1i64y1y7mm?p=25
python manage.py migrate
After creation, the following interface will appear
python manage.py createsuperuser
127.0.0.1:8000/admin
stay api Under folder models.py Add the following to the file
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')
Add the following sentence in djangoProject20211114 Under the settings.py Of INSTALLED_APPS
‘api.apps.ApiConfig’,
(api Is the name of the function you created )
/ root directory /settings.py
Adding INSTALLED_APPS = [
The name of the function .apps. The name of the function Config
]
python manage.py makemigrations api
python manage.py makemigrations The name of the function
pip install Pillow
python manage.py migrate
Add the following sentence in djangoProject20211114 Under the 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)
Solution :
stay djangoProject20211114 Under the settings.py in , Add at the end of the file
MEDIA_URL = "/upimg/"
MEDIA_ROOT = os.path.join(BASE_DIR, "upimg")
upimg This name can be assigned by yourself
stay djangoProject20211114 Under the urls.py Configuration is introduced in
from djangoProject20211114.settings import MEDIA_ROOT
from django.views.static import serve
url(r'^upimg/(?P<path>.*)$', serve, {
"document_root": MEDIA_ROOT})
Reference link :https://www.bilibili.com/video/BV1oU4y1w7PF?spm_id_from=333.999.0.0