1、创建项目
django-admin startproject <项目名>
2、setting配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"static")
]
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
3、views返回类型
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.template import loader
def index(request):
return HttpResponse('<h1>Two index</h1>')
def get_students(request):
students = Student.objects.all()
context = {
"students": students
}
return render(request, 'student_list.html', context=context)
def index(request):
return redirect('/home/')
4、创建APP
python manage.py startapp <app名>
在setting中注册app
INSTALLED_APPS = [
'<app名>'
]
5、创建路由(urls)
from django.urls import path
urlpatterns = [
path('index/', views.index),
path('two/',include('app02.urls')),
]
6、运行项目
python manage.py runserver 127.0.0.1:8000