Django项目: 6.新闻详情页

论坛 期权论坛     
选择匿名的用户   2021-6-2 18:50   726   0
<div class="blogpost-body" id="cnblogs_post_body">
<h2 class="md-end-block md-heading md-focus"><span class="md-plain md-expand">一、功能需求分析</span></h2>
<h4 class="md-end-block md-heading"><span class="md-plain">1.功能</span></h4>
<ol class="ol-list"><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">新闻详情</span></p> </li><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">加载评论功能</span></p> </li><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">添加评论功能</span></p> </li></ol>
<h2 class="md-end-block md-heading"><span class="md-plain">二、新闻详情页</span></h2>
<h4 class="md-end-block md-heading"><span class="md-plain">1.业务流程分析</span></h4>
<p class="md-end-block md-p"><span class="md-plain">业务流程:</span></p>
<ol class="ol-list"><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">判断前端传递新闻id是否为空,是否为整数,是否存在</span></p> </li></ol>
<h4 class="md-end-block md-heading"><span class="md-plain">2.接口设计</span></h4>
<ol class="ol-list"><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">接口说明:</span></p> </li></ol>
<table class="md-table"><thead><tr class="md-end-block"><th><span class="td-span"><span class="md-plain">类目</span></span></th><th><span class="td-span"><span class="md-plain">说明</span></span></th></tr></thead><tbody><tr class="md-end-block"><td><span class="td-span"><span class="md-plain">请求方法</span></span></td><td><span class="td-span"><span class="md-plain">GET</span></span></td></tr><tr class="md-end-block"><td><span class="td-span"><span class="md-plain">url定义</span></span></td><td><span class="td-span"><code>/news/&lt;int:news_id&gt;/</code></span></td></tr><tr class="md-end-block"><td><span class="td-span"><span class="md-plain">参数格式</span></span></td><td><span class="td-span"><span class="md-plain">url路径参数</span></span></td></tr></tbody></table>
<ol class="ol-list" start="2"><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">参数说明:</span></p> </li></ol>
<table class="md-table"><thead><tr class="md-end-block"><th><span class="td-span"><span class="md-plain">参数名</span></span></th><th><span class="td-span"><span class="md-plain">类型</span></span></th><th><span class="td-span"><span class="md-plain">是否必须</span></span></th><th><span class="td-span"><span class="md-plain">描述</span></span></th></tr></thead><tbody><tr class="md-end-block"><td><span class="td-span"><span class="md-plain">news_id</span></span></td><td><span class="td-span"><span class="md-plain">整数</span></span></td><td><span class="td-span"><span class="md-plain">是</span></span></td><td><span class="td-span"><span class="md-plain">新闻id</span></span></td></tr></tbody></table>
<ol class="ol-list" start="3"><li class="md-list-item"> <p class="md-end-block md-p"><span class="md-plain">返回结果:</span></p> <p class="md-end-block md-p"><span class="md-plain">html页面,直接通过模板渲染的方式实现</span></p> </li></ol>
<h4 class="md-end-block md-heading"><span class="md-plain">3.后端代码</span></h4>
<p class="md-end-block md-p"><span class="md-plain">视图</span></p>
<div class="cnblogs_code">
  <pre class="blockcode"><span style="color:#008000;">#</span><span style="color:#008000;"> 在news/views.py中定义如下视图</span>
<span style="color:#0000ff;">class</span><span style="color:#000000;"> NewDetailView(View):
    </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> get(self, request, news_id):
        news </span>&#61; News.objects.select_related(<span style="color:#800000;">&#39;</span><span style="color:#800000;">tag</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">author</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">).only(
            </span><span style="color:#800000;">&#39;</span><span style="color:#800000;">title</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">content</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">update_time</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">tag__name</span><span style="color:#800000;">&#39;</span>, <span style="color:#800000;">&#39;</span><span style="color:#800000;">author__username</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">).filter(
            is_delete</span>&#61;False, id&#61;<span style="color:#000000;">news_id).first()
        </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> news:
            </span><span style="color:#0000ff;">return</span> render(request, <span style="color:#800000;">&#39;</span><span style="color:#800000;">news/news_detail.html</span><span style="color:#800000;">&#39;</span>, context&#61;<span style="color:#000000;">{
                </span><span style="color:#800000;">&#39;</span><span style="color:#800000;">news</span><span style="color:#800000;">&#39;</span><span style="color:#000000;">: news
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP