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

django全套配置

編輯: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', #添加放到CommonMiddleware上面
# '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') #放在STATIC_URL下面,指定圖片保存目錄
# 跨域增加忽略,放在最下面
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',
)
項目根目錄下的urls.py
from django.contrib import admin
from django.urls import path,include
#導兩個包
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下的urls.py
from rest_framework.routers import DefaultRouter
from myapp import views
router = DefaultRouter()
router.register('',views.HelloWorldViewSet,basename='hello')
urlpatterns = router.urls
app下的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 #分頁
import datetime
import os
from andun import settings
import re
import requests
from selenium import webdriver
import time
# Create your views here.
#初始化驅動
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):
'''操作登陸後的頁面'''
code=request.data.get('code')
return Response({'code':'200'})
app下的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):
'''用戶表'''
name=models.CharField(verbose_name='用戶名字',max_length=50,primary_key=True,db_index=True)
tag=models.CharField(verbose_name='標簽',max_length=50,db_index=True,default='')
create_time=models.CharField(verbose_name='創建時間戳',max_length=50,default=0)
class Meta:
managed=True
verbose_name_plural='用戶表'
def __str__(self):
return self.name
class Group(models.Model):
'''群聊表'''
send=models.ForeignKey(User,on_delete=CASCADE)
recive=models.CharField(verbose_name='接收方',max_length=50)
group_id=models.IntegerField(verbose_name='群聊編號')
msgtime=models.IntegerField(verbose_name='發送時間')
msgtype=models.CharField(verbose_name='消息類型',max_length=50)
msg=models.CharField(verbose_name='消息內容',max_length=2550)
class Meta:
managed=True
verbose_name_plural='群聊表'
def __str__(self):
return self.send.name
class Alone(models.Model):
'''獨聊表'''
send=models.ForeignKey(User,on_delete=CASCADE)
recive=models.CharField(verbose_name='接收方',max_length=50)
msgtime=models.IntegerField(verbose_name='發送時間')
msgtype=models.CharField(verbose_name='消息類型',max_length=50)
msg=models.CharField(verbose_name='消息內容',max_length=2550)
class Meta:
managed=True
verbose_name_plural='獨聊表'
def __str__(self):
return self.send.name
class File(models.Model):
'''文件表'''
user=models.ForeignKey(User,on_delete=CASCADE)
file_path=models.CharField(verbose_name='文件路徑',max_length=500)
file_classify=models.CharField(verbose_name='文件類型',max_length=50)
class Meta:
managed=True
verbose_name_plural='文件表'
def __str__(self):
return self.user.name
class Identity(models.Model):
'''身份表'''
identity=models.CharField(verbose_name='身份',max_length=30,primary_key=True,db_index=True)
class Meta:
managed=True
verbose_name_plural='身份表'
def __str__(self):
return self.identity
class Account(models.Model):
'''賬號表'''
account=models.CharField(verbose_name='賬號',max_length=20,primary_key=True,db_index=True)
password=models.CharField(verbose_name='密碼',max_length=20)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural='賬號表'
def __str__(self):
return self.account
class File_Privilege(models.Model):
'''文件權限表'''
file_path=models.CharField(verbose_name='文件路徑',max_length=500,primary_key=True)
user=models.ForeignKey(User,on_delete=CASCADE)
file_classify=models.CharField(verbose_name='文件類型',max_length=50)
name=models.CharField(verbose_name='文件名',max_length=500)
file_grade=models.IntegerField(verbose_name='文件層級',default=1)
last_file=models.CharField(verbose_name='上一級文件目錄',max_length=500,default='')
is_folder=models.BooleanField(verbose_name='是否為文件夾',default=False)
class Meta:
managed=True
verbose_name_plural='文件權限表'
def __str__(self):
return self.file_path
class Account_File_Privilege(models.Model):
'''帳號文件權限表'''
file_path=models.ForeignKey(File_Privilege,on_delete=CASCADE)
account=models.ForeignKey(Account,on_delete=CASCADE)
add=models.BooleanField(verbose_name='是否可以增加文件',default=False)
rm=models.BooleanField(verbose_name='是否可以刪除文件',default=False)
edit=models.BooleanField(verbose_name='是否可以編輯文件',default=False)
class Meta:
managed=True
verbose_name_plural='賬號文件權限表'
def __str__(self):
return self.file_path.file_path+'——>'+self.account.account
class Account_Navigation_Privilege(models.Model):
'''賬號導航欄權限表'''
name=models.CharField(verbose_name='導航名',max_length=500)
account=models.ForeignKey(Account,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural='賬號導航欄權限表'
def __str__(self):
return self.name+'——>'+self.account.account
class Identity_File_Privilege(models.Model):
'''身份文件權限表'''
file_path=models.ForeignKey(File_Privilege,on_delete=CASCADE)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
add=models.BooleanField(verbose_name='是否可以增加文件',default=False)
rm=models.BooleanField(verbose_name='是否可以刪除文件',default=False)
edit=models.BooleanField(verbose_name='是否可以編輯文件',default=False)
class Meta:
managed=True
verbose_name_plural='身份文件權限表'
def __str__(self):
return self.file_path.file_path+'——>'+self.identity_id.identity
class Identity_Navigation_Privilege(models.Model):
'''身份導航欄權限表'''
name=models.CharField(verbose_name='導航名',max_length=500)
identity_id=models.ForeignKey(Identity,on_delete=CASCADE)
class Meta:
managed=True
verbose_name_plural='身份導航欄權限表'
def __str__(self):
return self.name+'——>'+self.identity_id.identity
class Food_File(models.Model):
'''文件上傳食物路徑表'''
file_path=models.CharField(verbose_name='服務器路徑',max_length=500)
file_truth=models.CharField(verbose_name='本地路徑',max_length=500,default='')
handle_res=models.BooleanField(verbose_name='是否通過',default=False)
is_handle=models.BooleanField(verbose_name='是否處理',default=False)
class Meta:
managed=True
verbose_name_plural='文件上傳食物路徑表'
def __str__(self):
return self.file_path
app下的admin.py
from django.contrib import admin
from myapp import models
# Register your models here.
###用戶表
admin.site.register(models.Account)
###群聊表
admin.site.register(models.Group)
###獨聊表
admin.site.register(models.Alone)
###文件表
admin.site.register(models.File)
###賬號文件權限表
admin.site.register(models.Account_File_Privilege)
###賬號導航欄權限表
admin.site.register(models.Account_Navigation_Privilege)
###身份文件權限表
admin.site.register(models.Identity_File_Privilege)
###身份導航欄權限表
admin.site.register(models.Identity_Navigation_Privilege)
###賬號表
admin.site.register(models.User)
###身份表
admin.site.register(models.Identity)
###文件權限表
admin.site.register(models.File_Privilege)
###食物文件表
admin.site.register(models.Food_File)

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