接上一个blog中在Django中创建一个运用:https://blog.youkuaiyun.com/dawn_02/article/details/80942393
我要是想在我的项目中创建一个templates怎么办?
很简单:
1.在blog项目中创建一个templates文件夹,
特别注意:尽量在templates下创建一个和运用同样名称的文件夹,之后在blog目录下的views.py中的地址为'blog/index.html'
2.在templates目录中写一个html文件 内容随便
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>hello blog222</h1>
</body>
</html>
3.在blog目录下的创建文件views.py 内容为:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request,'index.html')
4.在blog目录下创建文件urls.py 内容为:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index/$', views.index),
]
特别注意:r'^index/$'中的/和$不能缺少
5.修改helloDawn目录下的settings.py中的INSTALLED_APPS 添加一个
'blog',
6.修改helloDawn目录下的urls.py中内容为:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls')),
]
7.运行项目,输入http://127.0.0.1:8080/blog/index/ 即可看到:hello blog222
大吉大利,今晚吃鸡!!!