Django中创建一个templates

接上一个blog中在Django中创建一个运用:https://blog.csdn.net/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


大吉大利,今晚吃鸡!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值