Django 之必知必会三板斧

yizhihongxing

一、HttpResponse


在django.http 模块中定义了HttpResponse 对象的API,HttpRequest 对象由Django 自动创建,不调用模板,直接返回数据。

1 在 app/views.py 中导入模块,添加对应的函数

from django.shortcuts import HttpResponse, render, redirect

# Create your views here.

def index(request):

    return HttpResponse("index page")

2 在 mysite/urls.py 中导入存放视图函数的py文件

from django.contrib import admin
from django.urls import path
from app import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'index/', views.index) # 路由与视图函数对应关系
]

3 启动项目

python manage.py runserver

 

二、redirect


1 redirect 重定向,重定向到本地html 页面或者其他的网页,打开之后会自动跳转。

2 还可以写别名,别名需要参数的话,就必须使用reverse 解析。

from django.shortcuts import HttpResponse, render, redirect

# Create your views here.

def index(request):
    
    # return redirect('http://www.baidu.com')
    # return redirect('/home/')
    return redirect('home')    # 别名方式

def home(request):
    return HttpResponse('home page')

3 在 mysite/urls.py

from django.contrib import admin
from django.urls import path
from app import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'index/', views.index), # 路由与视图函数对应关系
    path(r'home/', views.home, name = 'home')
]

 

三、render


返回html 页面,并且返回给浏览器之前还可以给html 文件传值

1 render 返回一个html 页面

from django.shortcuts import HttpResponse, render, redirect

# Create your views here.

def index(request):
    """
    :param request: 请求相关的所有数据对象
    """
    # return render(request, 'index.html')
    
    data = "iyuyixyz"
    # 将数据对象展示到页面上
    return render(request, 'index.html', locals())

2 进入templates 文件夹下创建一个 templates/index.html 页面,并写入内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>index page</h3>
    <h3>{{data}}</h3>
</body>
</html>

视图函数必须要返回一个HttpResponse对象,研究三者的源码即可得处结论。

The view app.views.index didn't return an HttpResponse object. It returned None instead.

Django 之必知必会三板斧

render 简单内部原理

from django.template import Template, Contextdef index(request):    res = Template('<h1>{{ info }}</h1>')    context = Context({'info': {'nickname':'iyuyixyz', 'email':'iyuyi.xyz@gmail.com'}})    result = res.render(context)    print(result)    return HttpResponse(result)

 

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Django 之必知必会三板斧 - Python技术站

(0)
上一篇 2023年4月2日
下一篇 2023年4月2日

相关文章

  • Django 之中间件

    Django 之中间件

    Python开发 2023年4月2日
    00
  • Django 简介和版本介绍

    一、简介 官方地址:https://www.djangoproject.com Django 是一个由Python 编写的具有完整架站能力的开源Web框架。使用 Django,只要很少的代码,开发人员就可以轻松地完成一个正式网站所需要的大部分内容,并进一步开发出全功能的Web服务。 Django 本身基于MVC 架构,即Model(模型)+View(视图)+…

    2023年4月2日
    00
  • Django 测试脚本

    一、测试脚本 Django 在创建项目时自动在应用下创建了tests.py,这个py文件可以作为测试文件;也可以在应用下手动创建一个py测试文件。 无论哪种方式,都需要提前书写以下代码。 from django.test import TestCase # Create your tests here. import os “””Run administra…

    2023年4月2日
    00
  • Django 出现 frame because it set X-Frame-Options to deny 错误

    一、背景 使用 django3 进行开发时,由于项目前端页面使用iframe框架,浏览器错误提示信息如下 Refused to display ‘http://127.0.0.1:8000/’ in a frame because it set ‘X-Frame-Options’ to ‘deny’.  根据提示信息发现是因为 X-Frame-Options…

    Python开发 2023年4月2日
    00
  • Django 使用Pycharm 创建工程

    一、Pycharm 创建Django 工程 事实上,我们一般不使用命令行,而是直接在Pycharm 中创建Django 项目。 Pycharm 是进行Django 开发的最佳 IDE,请大家自行安装,建议使用最新的专业版本。 (非专业版的Pycharm 不提供Django 开发模块,非最新版的 Pycharm 可能对最新的 Python 或者 Django …

    2023年4月2日
    00
  • Django ORM 事务和查询优化

    一、事务操作 模块 from django.db import transaction 1 开启事务:with transaction.atomic() from django.db import transaction class MyView(View): def post(self, request): ### 在with代码块中开启事务,出了with…

    Python开发 2023年4月2日
    00
  • Django 聚合查询 分组查询 F与Q查询

    一、聚合查询 需要导入模块:from django.db.models import Max, Min, Sum, Count, Avg 关键语法:aggregate(聚合结果别名 = 聚合函数(参数)) 查询结果:使用聚合函数,从每一个组中获取结果:字典 注意点: 1 聚合函数必须在分组之后才能使用 2 没有分组,即默认整体就是一组 3 查询结果为 普通字…

    Python开发 2023年4月2日
    00
  • Django ORM 实现数据的单表 增删改查

    一、配置环境 1 Django 连接数据库(MySQL) DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.mysql’, ‘NAME’: ‘xyz’, ‘USER’:’root’, ‘PASSWORD’:’root’, ‘HOST’:’IP地址’, ‘PORT’:3306, ‘CHARSET’…

    Python开发 2023年4月2日
    00
合作推广
合作推广
分享本页
返回顶部