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'),
]