首页 > 其他分享 >django基本流程

django基本流程

时间:2022-12-20 15:55:53浏览次数:55  
标签:基本 settings 流程 auth django path contrib import

创建项目

django-admin startproject web
cd web
python manage.py startapp weblist

生成迁移文件

python manage.py makemigrations

生成迁移数据

   python manage.py migrate

运行

python manage.py runserver

settings配置

"""
Django settings for web project.

Generated by 'django-admin startproject' using Django 4.0.2.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-o%&42(%9p0r(8(_jn#re&7b!$_ln45s$(-7-a_*((!#h+k*s_b'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'weblist.templatetags',
    'weblist',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'web.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # 'DIRS': [],
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'web.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'jiale',
        'POST':3306,
        'HOST':'localhost',
        'USER':'root',
        'PASSWORD':'123456'
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
UPLOAD_FILE = os.path.join(BASE_DIR,'upload')

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

# DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

路由分发

#主路由
from django.contrib import admin
from django.urls import path,include
from django.views.static import serve
from web import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('weblist.urls')),
    path('upload/<path>',serve,{'document_root':settings.UPLOAD_FILE})

]


#子路由

# from django.contrib import admin
from django.urls import path
from weblist import views

urlpatterns = [
    # path('admin/', admin.site.urls),
    path('login/', views.Login.as_view()),
    path('index/', views.Index.as_view()),
    path('reg/', views.Reg.as_view()),

]

注册

from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from rest_framework.views import APIView
from django.views.generic import View
from weblist.models import *
# from weblist.myserializer import *
import os
from web import settings


# 新闻资讯注册
import re
class Reg(View):
    def get(self,request):
        return render(request,'reg.html')
    def post(self,request):
        name = request.POST.get('name')
        passwd = request.POST.get('passwd')
        phone = request.POST.get('phone')
        if not all([name,passwd,phone]):
            mes='请输入完整信息'
        else:
            user = User.objects.filter(name=name).first()
            if user:
                mes='用户已存在'
            else:
                user = User(name=name,passwd=passwd,phone=phone)
                user.save()
                return redirect('/api/login')
        return render(request,'login.html',locals())

标签:基本,settings,流程,auth,django,path,contrib,import
From: https://www.cnblogs.com/djl-0628/p/16994363.html

相关文章

  • django组件使用
    django内置序列化组件(drf前身)'''前后端分离的项目视图函数只需要返回json格式的数据即可'''fromapp01importmodelsfromdjango.httpimportJsonResponsedefa......
  • django模型层之models入门篇(filed options)
    一、新建一个django项目,注册两个app分别为tournament、comment,其中models的代码分别为:#tournament/models.pyfromdjango.dbimportmodelsclassClub(models.Model......
  • itop2k1000开发板Makefile基本语法wildcard 函数
    格式:$(wildcardPATTENR)功能:展开指定的目录举例:在/home/topeet/test目录有一个“a.c”的c文件和一个test的文件夹,在/home/topeet/test/test文件夹下有一个......
  • itop2k1000开发板Makefile基本语法wildcard 函数
    格式:$(wildcardPATTENR)功能:展开指定的目录举例:在/home/topeet/test目录有一个“a.c”的c文件和一个test的文件夹,在/home/topeet/test/test文件夹下有一个......
  • 基本运算符
    基本运算符运算符Java语言支持如下运算符:算术运算符:+,-,*,/,%,++,--赋值运算符:=关系运算符:>,<,>=,<=,==,!=,instanceof逻辑运算符:&&,||,!位运算符:&,|,^,~,>>,<<,>>>(了解!!!)条......
  • docker介绍及基本操作命令
    docker1.介绍#虚拟化虚拟化是一种资源管理技术,打破实体结构间的不可切割的障碍。虚拟化技术:VMware,VirtualBox,阿里云服务器,docker虚拟化技术#虚拟化相关名词 -......
  • 【python】mac+appium+python的环境配置完整流程
    一、概述(如果电脑已有可以跳过):1、安装JavaJDK2、安装Homebrew3、安装Python34、安装Git5、安装AdroidSDK(要测试ios,还需要安装Xcode)6、安装Node.js7、安装AppiumSe......
  • django框架(七)
    (3)Q查询进阶操作研究查询条件的左边是什么?字段名还是变量名?models.Book.objects.filter(pk=1)发现是变量名如果想让左边不是变量名而是字段名,则需要用到Q查询Q:还可......
  • Django之Ajax
    什么是Ajax?ajax全名:asyncjavascriptandXML(异步JavaScript和XML)是前后台交互的能⼒,也就是我们客户端给服务端发送消息的⼯具,以及接受响应的⼯具AJAX不是新......
  • 积分基本定理的几何说明
    对于微积分的核心概念,个人认为有一句话描述的非常到位,"导数是变化的原因,积分是变化的结果"!书上对微积分基本定理的描述以及证明如下:设 在闭区间上连续,是在上的一个原函数,则:......