【Django2.0学习笔记】04.使用模板显示内容

04、使用模板显示内容

1、查看文章页面

如何通过一个处理方法获取文章唯一标识
在这里插入图片描述

article\views.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def article_detail(request, article_id):
	return HttpResponse("文章id:%s" % article_id)

mysite\mysite\urls.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path
from . import views
from article.views import article_detail

urlpatterns = [
    path(r'admin/', admin.site.urls),
	path('', views.index),
	path('article/<int:article_id>', article_detail, name='article_detail'),
]

【遇到的问题】访问http://localhost:8000/article/2时,结果还是没有显示article的内容,一直只显示hello world内容
在这里插入图片描述
【可能的原因】
在这里插入图片描述
在这里插入图片描述
【解决方法】:升级Django版本,用法Django2.0重新新建项目
在这里插入图片描述
再访问http://localhost:8000/article/2时:
在这里插入图片描述

2、objects

  • 模型的objects是获取或操作模型的对象
    • Article.objects.get(条件)
    • Article.objects.all()
    • Article.objects.filter(条件)

在这里插入图片描述

article\views.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Article

# Create your views here.
def article_detail(request, article_id):
	article = Article.objects.get(id=article_id)
	return HttpResponse("<h2>文章标题:%s</h2><br>文章内容:%s" % (article.title, article.content))

访问 http://localhost:8000/article/2,显示如下:
在这里插入图片描述

如果访问 http://localhost:8000/article/5,会发生什么情况呢?——会显示不存在,因为我们数据只有三条Article object1、2、3。所以我们要处理这个错误,需要在程序里进行判断
在这里插入图片描述
views.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
from django.shortcuts import render
from django.http import HttpResponse, Http404
from .models import Article

# Create your views here.
def article_detail(request, article_id):
	try:
		article = Article.objects.get(id=article_id)
	except:
		raise Http404("not exist")
	return HttpResponse("<h2>文章标题:%s</h2><br>文章内容:%s" % (article.title, article.content))

在这里插入图片描述

3、使用模板

  • 前端页面和后端代码分离,减低耦合性
    在这里插入图片描述

新建文件夹 templates,这个文件夹已经规定好了,在mysite–settings里面的TEMPLATES
在这里插入图片描述
在templates里新建article_detail.html,比如我们在之前的网页内容基础上,在title和content中间加一条横线

mysite\article\templates\article_detail.html:

<html>
<head>
</head>
<body>
	<h2>{{ article_obj.title }}</h2>
	<hr>
	<p>{{ article_obj.content }}</p>
</body>
</html>

mysite\article\views.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponse, Http404
from .models import Article

# Create your views here.
def article_detail(request, article_id):
	try:
		article = Article.objects.get(id=article_id)
		context = {}
		context['article_obj'] = article
		return render(request, 'article_detail.html', context)
		# 或者 return render_to_response('article_detail.html', context)
	except:
		raise Http404("not exist")
	# return HttpResponse("<h2>文章标题:%s</h2><br>文章内容:%s" % (article.title, article.content))

在这里插入图片描述
简洁优化写法,用 get_object_or_404
mysite\article\views.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
# from django.shortcuts import render, 
from django.shortcuts import render_to_response, get_object_or_404
# from django.http import HttpResponse, Http404
from .models import Article

# Create your views here.
def article_detail(request, article_id):
	'''
	try:
		article = Article.objects.get(id=article_id)
		context = {}
		context['article_obj'] = article
		return render(request, 'article_detail.html', context)
		# 或者 return render_to_response('article_detail.html', context)
	except:
		raise Http404("not exist")
	# return HttpResponse("<h2>文章标题:%s</h2><br>文章内容:%s" % (article.title, article.content))
	'''
	article = get_object_or_404(Article, pk=article_id)
	context = {}
	context['article_obj'] = article
	return render_to_response('article_detail.html', context)

4、获取文章列表

  • 文章列表:
    • 文章1
    • 文章2
    • 文章3
      在这里插入图片描述

在mysite\article\templates 目录下新建 article_list.html,代码如下,将文章罗列出来:

<html>
<head>
</head>
<body>
	{% for article in articles %}
		<p>{{ article.title }}</p>
	{% endfor %}
</body>
</html>

修改 mysite\article\views.py如下,新增article_list处理方法:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\views.py
from django.shortcuts import render_to_response, get_object_or_404
from .models import Article

# Create your views here.
def article_detail(request, article_id):
	article = get_object_or_404(Article, pk=article_id)
	context = {}
	context['article_obj'] = article
	return render_to_response('article_detail.html', context)

def article_list(request):
	articles = Article.objects.all()
	context = {}
	context['articles'] = articles
	return render_to_response("article_list.html", context)

修改 mysite\mysite\urls.py如下,新增一条article_list的路由:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path
from . import views
from article.views import article_detail, article_list

urlpatterns = [
    path('admin/', admin.site.urls),
	path('', views.index),
	path('article/<int:article_id>', article_detail, name='article_detail'),
	path('article/', article_list, name='article_list'),
]

在这里插入图片描述
继续完善mysite\article\templates\article_list.html,给文章加上链接:

<html>
<head>
</head>
<body>
	{% for article in articles %}
		<!--<a href="/article/{{ article.pk }}">{{ article.title }}</a>-->
		<a href="{% url 'article_detail' article.pk %}">{{ article.title }}</a> <!--用别名name-->
		<p>{{ article.title }}</p>
	{% endfor %}
</body>
</html>

在这里插入图片描述

5、总urls包含app的urls

  • 总urls:
    • app1 urls
    • app2 urls
    • app3 urls
      在这里插入图片描述

在article这个app的目录下新建urls.py,把项目下总的urls中关于app的url剪切过来
总urls.py代码如下,新增一行代码path('article/', include('article.urls')),表示总urls包含某app的urls:
mysite\urls.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
from article.views import article_detail, article_list

urlpatterns = [
    path('admin/', admin.site.urls),
	path('', views.index),
	path('article/', include('article.urls'))	# # 改动
	# path('article/<int:article_id>', article_detail, name='article_detail'),
	# path('article/', article_list, name='article_list'),
]

新增的article的urls.py如下,将与article这个app相关的urls代码剪切进来:
mysite\article\urls.py:

# C:\Users\12482\Desktop\py_learn\test_django\mysite\article\urls.py
from django.urls import path
from . import views
	
urlpatterns = [
	# localhost:8000/article/
	path('', views.article_list, name='article_list'),
	# localhost:8000/article/3
	path('<int:article_id>', views.article_detail, name='article_detail'),	 
]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值