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

Django complete configuration

編輯:Python
settings.py
import os
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'myapp'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware', # Add to CommonMiddleware above
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'andun',
'USER':'root',
'PASSWORD':'pert',
'HOST':'localhost',
'PORT':'3306'
}
}
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
UPLOAD_FILE= os.path.join(BASE_DIR,'upload') # Put it in STATIC_URL below , Specify the picture saving Directory
# Cross domain add ignore , At the bottom
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'http://192.168.101.30:8000',
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
In the root directory of the project urls.py
from django.contrib import admin
from django.urls import path,include
# Two bags
from django.views.static import serve
from andun import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('myapp.urls')),
path('upload/<path>',serve,{'document_root':settings.UPLOAD_FILE})
]
app Under the urls.py
from rest_framework.routers import DefaultRouter
from myapp import views
router = DefaultRouter()
router.register('',views.HelloWorldViewSet,basename='hello')
urlpatterns = router.urls
app Under the views.py
from email import header
from lib2to3.pgen2 import driver
from django.db.models import Q, query
from requests import head
from myapp import models
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import pagination # Pagination
import datetime
import os
from andun import settings
import re
import requests
from selenium import webdriver
import time
# Create your views here.
# Initialize drive
driver=webdriver.Chrome()
driver.minimize_window()
class HelloWorldViewSet(GenericViewSet):
@action(detail=False,methods=['GET'])
def get_code(self,request):
return Response({'code':'200'})
@action(detail=False,methods=['POST'])
def login(self,request):
''' Operate the login page '''
code=request.data.get('code')
return Response({'code':'200'})
app Under the models.py
from django.db import models
from django.db.models.deletion import CASCADE
from django.db.models.fields import Field
# Create your models here.
class User(models.Model):
''' User table '''
name=models.CharField(verbose_name=' User name ',max_length=50,primary_key=True,db_index=True)
tag=models.CharField(verbose_name=' label ',max_length=50,db_index=True,default='')
create_time=models.CharField(verbose_name=' Create timestamps ',max_length=50,default=0)
class Meta:
managed=True
verbose_name_plural=' User table '
def __str__(self):
return self.name
class Group(models.Model):
''' Group chat table '''
send=models.ForeignKey(User,on_delete=CASCADE)
recive=models.CharField(verbose_name=' The receiving party ',max_length=50)
group_id=models.IntegerField(verbose_name=' Group chat number ')
msgtime=models.IntegerField(verbose_name=' Send time ')
msgtype=models.CharField(verbose_name=' Message type ',max_length=50)
msg=models.CharField(verbose_name=' The message content ',max_length=2550)
class Meta:
managed=True
verbose_name_plural=' Group chat table '
def __str__(self):
return self.send.name
class Alone(models.Model):
''' Talk alone '''
send=models.ForeignKey(User,on_delete=CASCADE)
recive=models.CharField(verbose_name=' The receiving party ',max_length=50)
msgtime=models.IntegerField(verbose_name=' Send time ')
msgtype=models.CharField(verbose_name=' Message type ',max_length=50)
msg=models.CharField(verbose_name=' The message content ',max_length=2550)
class Meta:
managed=True
verbose_name_plural=' Talk alone '
def __str__(self):
return self.send.name
class File(models.Model):
''' Document list '''
user=models.ForeignKey(User,on_delete=CASCADE)
file_path=models.CharField(verbose_name=' File path ',max_length=500)
file_classify=models.CharField(verbose_name=' file type ',max_length=50)
class Meta:
managed=True
verbose_name_plural=' Document list '
def __str__(self):
return self.user.name
class Identity(models.Model):
''' Identity table '''
identity=models.CharField(verbose_name=' identity ',max_length=30,primary_key=True,db_index=True)
class Meta:
managed=True
verbose_name_plural=' Identity table '
def __str__(self):
return self.identity
class Account(models.Model):
''' Account table '''
account=models.CharField(verbose_name=' account number ',max_length=20,primary_key=True,db_index=True)
password=models.CharField(verbose_name=' password ',max_length=20)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural=' Account table '
def __str__(self):
return self.account
class File_Privilege(models.Model):
''' Document authority table '''
file_path=models.CharField(verbose_name=' File path ',max_length=500,primary_key=True)
user=models.ForeignKey(User,on_delete=CASCADE)
file_classify=models.CharField(verbose_name=' file type ',max_length=50)
name=models.CharField(verbose_name=' file name ',max_length=500)
file_grade=models.IntegerField(verbose_name=' Document level ',default=1)
last_file=models.CharField(verbose_name=' Upper level file directory ',max_length=500,default='')
is_folder=models.BooleanField(verbose_name=' Is it a folder ',default=False)
class Meta:
managed=True
verbose_name_plural=' Document authority table '
def __str__(self):
return self.file_path
class Account_File_Privilege(models.Model):
''' Account file permission table '''
file_path=models.ForeignKey(File_Privilege,on_delete=CASCADE)
account=models.ForeignKey(Account,on_delete=CASCADE)
add=models.BooleanField(verbose_name=' Whether you can add files ',default=False)
rm=models.BooleanField(verbose_name=' Whether the file can be deleted ',default=False)
edit=models.BooleanField(verbose_name=' Whether the file can be edited ',default=False)
class Meta:
managed=True
verbose_name_plural=' Account file permission table '
def __str__(self):
return self.file_path.file_path+'——>'+self.account.account
class Account_Navigation_Privilege(models.Model):
''' Account navigation bar permission table '''
name=models.CharField(verbose_name=' Navigation name ',max_length=500)
account=models.ForeignKey(Account,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural=' Account navigation bar permission table '
def __str__(self):
return self.name+'——>'+self.account.account
class Identity_File_Privilege(models.Model):
''' Identity file permission table '''
file_path=models.ForeignKey(File_Privilege,on_delete=CASCADE)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
add=models.BooleanField(verbose_name=' Whether you can add files ',default=False)
rm=models.BooleanField(verbose_name=' Whether the file can be deleted ',default=False)
edit=models.BooleanField(verbose_name=' Whether the file can be edited ',default=False)
class Meta:
managed=True
verbose_name_plural=' Identity file permission table '
def __str__(self):
return self.file_path.file_path+'——>'+self.identity_id.identity
class Identity_Navigation_Privilege(models.Model):
''' Identity navigation bar permission table '''
name=models.CharField(verbose_name=' Navigation name ',max_length=500)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural=' Identity navigation bar permission table '
def __str__(self):
return self.name+'——>'+self.identity_id.identity
class Food_File(models.Model):
''' File upload food path table '''
file_path=models.CharField(verbose_name=' Server path ',max_length=500)
file_truth=models.CharField(verbose_name=' The local path ',max_length=500,default='')
handle_res=models.BooleanField(verbose_name=' Whether to pass ',default=False)
is_handle=models.BooleanField(verbose_name=' Whether to deal with ',default=False)
class Meta:
managed=True
verbose_name_plural=' File upload food path table '
def __str__(self):
return self.file_path
app Under the admin.py
from django.contrib import admin
from myapp import models
# Register your models here.
### User table
admin.site.register(models.Account)
### Group chat table
admin.site.register(models.Group)
### Talk alone
admin.site.register(models.Alone)
### Document list
admin.site.register(models.File)
### Account file permission table
admin.site.register(models.Account_File_Privilege)
### Account navigation bar permission table
admin.site.register(models.Account_Navigation_Privilege)
### Identity file permission table
admin.site.register(models.Identity_File_Privilege)
### Identity navigation bar permission table
admin.site.register(models.Identity_Navigation_Privilege)
### Account table
admin.site.register(models.User)
### Identity table
admin.site.register(models.Identity)
### Document authority table
admin.site.register(models.File_Privilege)
### Food paperwork
admin.site.register(models.Food_File)

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