马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kylin121380 于 2020-4-11 12:45 编辑
django3.0.3
生成项目:$ django-admin startproject try
检查是否正常:$ cd try
$ python manage.py runserver
打开127.0.0.1:8000可以看到成功界面
创建应用:$ python manage.py startapp server
在server目录下创建urls.py文件,打开views.py,输入以下代码:from django.http import HttpResponse
def index(request):
return HttpResponse('this is a test')
打开urls,输入:from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name = 'server_index'),
]
返回上一层目录进入try目录,打开urls,输入:from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('server/',include('server.urls')),
path('admin/', admin.site.urls),
]
保存后$ python manage.py runserver
,进入127.0.0.1:8000/server可看到this is a test
在try项目根目录下新建templates文件夹,进入templates文件夹新建materials文件夹,其中新建两个html文件,分别为index.html,index_link.html,内容分别如下:<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>this is a test</title>
</head>
<body>
<p>one two three</p>
<a href = "/templates/materials/index_link.html">点此跳转</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>this is a link</title>
</head>
<body>
<p>three two one</p>
</body>
</html>
打开try下的settings,添加相关配置如下:STATIC_URL = '/templates/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'templates'),
)
将TEMPLATES中的dir修改为'DIRS': ['templates']如下:TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['materials'],
'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,include
urlpatterns = [
path('server/',include(('server.urls','server'),namespace = 'server_index')),
path('admin/', admin.site.urls),
]
进入server目录
将views.py修改为:from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'materials/index.html')
目录及运行后如下:
|