标签:nal spl pass r文件 dirname ica response similar 静态
上下文管理器
django提取context中的数据去供模板调用
需求: 所有的页面都需要一个特定的变量
本质: python函数 , 接收一个HttpRequest对象的参数 , 且返回的必须是一个字典
定义上下文管理器
文件名命名不受限, 放置的路径也不受拘束, 可以放在django项目下的任意位置
1、在user文件夹下创建文件process_content.py
1 from . import models 2 3 def category_process(request): 4 print(‘category_process......‘) 5 categories = models.Category.objects.filter(is_delete=False) 6 return {‘nav‘:categories}
2、配置setting.py文件
1 TEMPLATES = [ 2 { 3 ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, 4 ‘DIRS‘: [os.path.join(BASE_DIR,‘templates‘)], 5 # os.path.join(BASE_DIR,‘templates‘)没了这句, 6 # 会显示django.template.exception.TemplateDoesNotExist: index.html 7 ‘APP_DIRS‘: True, 8 ‘OPTIONS‘: { 9 ‘context_processors‘: [ 10 ‘django.template.context_processors.debug‘, 11 ‘django.template.context_processors.request‘, 12 ‘django.contrib.auth.context_processors.auth‘, 13 ‘django.contrib.messages.context_processors.messages‘, 14 ‘user.process_content.category_process‘ 15 ], 16 }, 17 }, 18 ]
1 """ 2 Django settings for dj_test project. 3 4 Generated by ‘django-admin startproject‘ using Django 2.1.5. 5 6 For more information on this file, see 7 https://docs.djangoproject.com/en/2.1/topics/settings/ 8 9 For the full list of settings and their values, see 10 https://docs.djangoproject.com/en/2.1/ref/settings/ 11 """ 12 13 import os 14 15 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 18 19 # Quick-start development settings - unsuitable for production 20 # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ 21 22 # SECURITY WARNING: keep the secret key used in production secret! 23 SECRET_KEY = ‘!nyr_@#lad3s*!_iwc68#9!_18f1x-!sygom9a#%ma!!kob+wh‘ 24 25 # SECURITY WARNING: don‘t run with debug turned on in production! 26 DEBUG = True 27 28 ALLOWED_HOSTS = [] 29 30 31 # Application definition 32 33 INSTALLED_APPS = [ 34 ‘django.contrib.admin‘, 35 ‘django.contrib.auth‘, 36 ‘django.contrib.contenttypes‘, 37 ‘django.contrib.sessions‘, 38 ‘django.contrib.messages‘, 39 ‘django.contrib.staticfiles‘, 40 ‘user.apps.UserConfig‘, 41 ] 42 43 MIDDLEWARE = [ 44 ‘django.middleware.security.SecurityMiddleware‘, 45 ‘django.contrib.sessions.middleware.SessionMiddleware‘, 46 ‘django.middleware.common.CommonMiddleware‘, 47 ‘django.middleware.csrf.CsrfViewMiddleware‘, 48 ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, 49 ‘django.contrib.messages.middleware.MessageMiddleware‘, 50 ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, 51 ] 52 53 ROOT_URLCONF = ‘dj_test.urls‘ 54 55 TEMPLATES = [ 56 { 57 ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, 58 ‘DIRS‘: [os.path.join(BASE_DIR,‘templates‘)], 59 # os.path.join(BASE_DIR,‘templates‘)没了这句, 60 # 会显示django.template.exception.TemplateDoesNotExist: index.html 61 ‘APP_DIRS‘: True, 62 ‘OPTIONS‘: { 63 ‘context_processors‘: [ 64 ‘django.template.context_processors.debug‘, 65 ‘django.template.context_processors.request‘, 66 ‘django.contrib.auth.context_processors.auth‘, 67 ‘django.contrib.messages.context_processors.messages‘, 68 ‘user.process_content.category_process‘ 69 ], 70 }, 71 }, 72 ] 73 74 WSGI_APPLICATION = ‘dj_test.wsgi.application‘ 75 76 77 # Database 78 # https://docs.djangoproject.com/en/2.1/ref/settings/#databases 79 80 DATABASES = { 81 ‘default‘: { 82 ‘ENGINE‘: ‘django.db.backends.sqlite3‘, 83 ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), 84 } 85 # ‘default‘: { 86 # ‘ENGINE‘: ‘django.db.backends.mysql‘, 87 # ‘NAME‘: ‘db1‘, 88 # ‘USER‘: ‘db1‘, 89 # ‘PASSWORD‘: ‘db1‘, 90 # ‘HOST‘: ‘db1‘, 91 # ‘PORT‘: ‘db1‘, 92 # } 93 } 94 95 96 # Password validation 97 # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators 98 99 AUTH_PASSWORD_VALIDATORS = [ 100 { 101 ‘NAME‘: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator‘, 102 }, 103 { 104 ‘NAME‘: ‘django.contrib.auth.password_validation.MinimumLengthValidator‘, 105 }, 106 { 107 ‘NAME‘: ‘django.contrib.auth.password_validation.CommonPasswordValidator‘, 108 }, 109 { 110 ‘NAME‘: ‘django.contrib.auth.password_validation.NumericPasswordValidator‘, 111 }, 112 ] 113 114 115 # Internationalization 116 # https://docs.djangoproject.com/en/2.1/topics/i18n/ 117 118 LANGUAGE_CODE = ‘zh-Hans‘ 119 120 TIME_ZONE = ‘Asia/Shanghai‘ 121 122 USE_I18N = True 123 124 USE_L10N = True 125 126 USE_TZ = False 127 128 129 # Static files (CSS, JavaScript, Images) 130 # https://docs.djangoproject.com/en/2.1/howto/static-files/ 131 132 STATIC_URL = ‘/static/‘ 133 134 TEMPLATE_DIRS = (os.path.join(BASE_DIR, ‘templates‘),) 135 136 STATICFILES_DIRS = ( 137 os.path.join(BASE_DIR,‘static‘), 138 ) # 用静态文件配置 139 140 # 多媒体路径path 141 MEDIA_ROOT = os.path.join(BASE_DIR,‘static‘) # 上传文件的路径
3、在views.py文件下,把HttpRequest返回的特定变量删除
1 import datetime 2 from django.shortcuts import render,HttpResponse 3 from .models import Category,Article 4 # Create your views here. 5 6 def index(request): 7 print(‘views...index‘) 8 article = Article.objects.filter(is_delete=False) 9 10 data = {‘articles‘:article} 11 return render(request,‘index.html‘,data) 12 13 def category(request,id): 14 print(‘views...index‘) 15 article = Article.objects.filter(is_delete=False,category_id=id) 16 17 data = {‘articles‘:article} 18 return render(request,‘index.html‘,data)
4、再次用浏览器打开index.html页面
标签:nal spl pass r文件 dirname ica response similar 静态
原文地址:https://www.cnblogs.com/zibinchen/p/12219422.html