目录
3、在MyDjango文件夹中新建templates文件夹,在templates文件夹里新建index.html文件
命令行中创建MyDjango项目
django-admin startproject MyDjango
进行创建的项目文件夹中
命令行中运行
cd MyDjango
创建项目应用index
命令行中运行
python manage.py startapp index
打MyDjango的配置文件setting.py,配置INSTALLED_APPS和模板文件夹
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'index'
- ]
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [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',
- ],
- },
- },
- ]
urls.py文件
- from django.contrib import admin
- from django.urls import path
- from index.views import index
-
- urlpatterns = [
- path('admin/', admin.site.urls),
- path('', index)
- ]
- from django.shortcuts import render
- def index(request):
- return render(request,'index.html')
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Hello World!!</title>
- </head>
- <body>
- <span>Hello World!!</span>
- </body>
- </html>
python manage.py runserver 8001
运行结果