程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django framework configuration

編輯:Python

title django Create an interface

1. Installation environment and framework django and djangorestframework

pip install django
pip install djangorestframework

2. Create a name api The interface of

python manage.py startapp api

3. modify djangoProject2021114 Of urls.py, It's not new api Inside

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')),
]

4. modify api Under folder views.py, Add the following two sentences and views object

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})

5. from djangoProject2021114 Make a copy of urls.py to api Under the folder , And modify it api Under folder urls.py file

from django.contrib import admin
from django.conf.urls import url, include
from api import views
urlpatterns = [
url(r'^login/',views.LoginView.as_view()),
]

6. The following error occurred

modify djangoProject20211114 Inside settings.py Medium INSTALLED_APPS, Add a sentence at the end ‘rest_framework’

7. Now, if the step is run directly, there will be the following error ,django_session It's because of the database , The modification scheme is as follows


Reference link :https://www.bilibili.com/video/BV1i64y1y7mm?p=25













django Connect to the built-in database db.sqlite3

1. Data and db.sqlite3 docking ,( Step by step from above , Those that have been docked are not needed )

python manage.py migrate


After creation, the following interface will appear

  • problem : No more dbsqlit3 Data table interface

  • The solution is as follows :

2. Create super admin

python manage.py createsuperuser

Enter the super administrator interface

127.0.0.1:8000/admin

3. function To configure ( Example : Picture model )

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')

4. Function introduction project

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
]

5. Function write to db.sqlit3 In the database

python manage.py makemigrations api
python manage.py makemigrations The name of the function
  • problem : There may be a mistake , If you look carefully, , It may be installation Pillow Group
  • Solution :
pip install Pillow

6. Update the database db.sqlit3

python manage.py migrate

7. Configure the background ( After configuration , You can manage in the super administrator interface )

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)

problem , The image was successfully imported by the super administrator , But I can't open it , It doesn't show

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})

8. give the result as follows

Reference link :https://www.bilibili.com/video/BV1oU4y1w7PF?spm_id_from=333.999.0.0


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved