django安装与配置
使用pip进行Django的安装:
pip install djangoconda install django
验证django安装成功
import django
django.__version__ ## django.get_version()
新建一个名称为blog的django项目(project),
D:\>cd web-project
D:\web-project>django-admin startproject blog
#web-project是一个专门存放web项目的目录
新建一个应用(app)
在blog项目下创建应用learn,命令如下:
D:\web-project\blog>python manage.py startapp learn
上述命令在D:\web-project\blog 目录下创建了一个 名为learn目录,目录内容下如图所示:
注:一个项目下可以有多个应用
把新定义的app加到settings.py中的INSTALL_APPS中
修改blog/blog/settings.py
INSTALLED_APPS = ('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','learn',
)
新建的 app 如果不加到 INSTALL_APPS 中的话, django 就不能自动找到app中的模板文件(app-name/templates/下的文件)和静态文件(app-name/static/中的文件)
启动服务器时,请注意 manage.py文件所在的位置
(mydjango) D:\web-project\blog>python manage.py runserver 8001
可以不指定端口,默认端口为 8000
视图
from django.shortcuts import render# coding:utf-8
from django.http import HttpResponsedef index(request):return HttpResponse(u"何物遍开幽谷?何物徒留芬芳?")
"""
URL configuration for blog project.The `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import pathfrom django.contrib import admin
from django.urls import path
from learn import views as learn_views # newurlpatterns = [path('', learn_views.index), # newpath('admin/', admin.site.urls),
]
建站
"""
Django settings for testHw project.Generated by 'django-admin startproject' using Django 4.2.7.For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""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.2/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-*^b6dmzwcj_$$qz1nav&i@**7-seyvazku3tq$^@vxz8y!$8$*'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',
]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 = 'testHw.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'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 = 'testHw.wsgi.application'# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',}
}# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validatorsAUTH_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.2/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/STATIC_URL = 'static/'# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','index',
]
from django.shortcuts import render# Create your views here.
from django.shortcuts import render# Create your views here.
def index(request):return render(request, 'index.html', context={'title':'首页'},status=500)
"""
URL configuration for testHw project.The `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views1. Add an import: from my_app import views2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views1. Add an import: from other_app.views import Home2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import pathfrom django.contrib import admin
from django.urls import pathfrom index import viewsurlpatterns = [path('admin/', admin.site.urls),path('', views.index),]