创建项目
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