1.使用render方法
return render(request,'index.html')
返回的页面内容是index.html的内容,但是url不变,还是原网页的url,(比如是login页面的返回方法,跳转后的url还是为login) 一刷新就返回去了
2.使用redirect方法
return redirect(request,'idnex.html')
直接跳转到index.html页面中,url为跳转后的页面url
补充知识:Django的View是如何工作的?
View (视图) 主要根据用户的请求返回数据,用来展示用户可以看到的内容(比如网页,图片),也可以用来处理用户提交的数据,比如保存到数据库中。Django的视图(View)通常和URL路由一起工作的。服务器在收到用户通过浏览器发来的请求后,会根据urls.py里的关系条目,去视图View里查找到与请求对应的处理方法,从而返回给客户端http页面数据。
当用户发来一个请求request时,我们通过HttpResponse打印出Hello, World!
# views.py from django.http import HttpResponse def index(request): return HttpResponse("Hello, World!")
下面一个新闻博客的例子。/blog/展示所有博客文章列表。/blog/article/<int:id>/展示一篇文章的详细内容。
# blog/urls.py from django.urls import path from . import views urlpatterns = [ path('blog/', views.index, name='index'), path('blog/article/<int:id>/', views.article_detail, name='article_detail'), ] # blog/views.py from django.shortcuts import render, get_object_or_404 from .models import Article # 展示所有文章 def index(request): latest_articles = Article.objects.all().order_by('-pub_date') return render(request, 'blog/article_list.html', {"latest_articles": latest_articles}) # 展示所有文章 def article_detail(request, id): article = get_object_or_404(Article, pk=id) return render(request, 'blog/article_detail.html', {"article": article})
模板可以直接调用通过视图传递过来的内容。
# blog/article_list.html {% block content %} {% for article in latest_articles %} {{ article.title }} {{ article.pub_date }} {% endfor %} {% endblock %} # blog/article_detail.html {% block content %} {{ article.title }} {{ article.pub_date }} {{ article.body }} {% endblock %}
以上这篇Python Django view 两种return的实现方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 潘秀琼.1994-木兰从军【名将】【WAV+CUE】
- 纯音入心系列纯音乐《古筝系列-当流行乐遇到古筝》1CD[MP3][1.9GB]
- 纯音入心系列纯音乐《精选古筝名曲100首》1CD[MP3][388.7MB]
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]