一、新闻详情接⼝设计
1、请求⽅式
2、请求参数:路径参数
3、响应结果:HTML
detail.html
二、新闻详情接⼝定义
定义路由
from django.urls import path,re_path
from . import views
urlpatterns=[
re_path('^$',views.IndexView.as_view(),name='index'),
re_path('^list/(?P<channel_id>\d+)/(?P<page_num>\d+)/$',views.IndexView.as_view()),
re_path('^detail/(?P<article_id>\d+)/',views.DetailView.as_view())
]
定义视图
class DetailView(View):
def get(self,request,article_id):
'''
新闻详情
:param request:
:param article_id:
:return:
'''
try:
article=Article.objects.get(id=article_id)
except Article.DoseNotExist:
return http.HttpResponseNotFound('未找到reticle_id这篇文章')
return render(request,'newsapp/detail.html',{'article':article})
三、detail.html文件接收后端返回的数据
<div class="post-item">
<div class="post-infor">
<div class="category-blog">
<a href="#">{{ }}</a>
</div>
<h3 class="post-title">
<a href="#">{{ article.title }}</a>
</h3>
<div class="main-info-post">
{{ article.content | safe }}
{% for imgobj in article.articleimage_set.all() %}
<img src="{{ imgobj.image.url }}" alt="">
{% endfor %}
</div>
</div>
</div>