django学习笔记(四)—简单跑通django项目

通过前三篇的了解,大概知道了django是干嘛的,现在可以开始操作了

首先在Helloworld目录下新建一个view.py文件

from django.http import HttpResponse

def hello(request):
    return HttpResponse("This is my first project")

然后在url.py文件下需要配置

from django.urls import path
from . import view

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'',view.hello)
]

【踩坑】需要把view引入,要不会报错

运行起来,页面上就会加载

上面还没有涉及到template

OK,可以直接分离页面和数据了,下面添加templates部分

(1)在项目下新建文件夹templates

【备注】此处踩坑把templates文件夹建到2里面,导致运行的时候怎么都找不到hello.html

 修改view.py文件

from django.shortcuts import render

def hello(request):
    context = {}
    context['hello'] = "hello world"
    print("ddd")#此处是为了查服务器一直异常的问题,定位到是问题是找不到hello.html
    return render(request, 'hello.html', context)

添加hello.html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>djangoTest</title>
</head>
<body>
    <h1>{{hello}}</h1>
    <p>Django 测试</p>
</body>
</html>

刷新页面http://127.0.0.1:8000/hello/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值