代码还没读完就踏上了修改功能的深坑。还好思路清晰,经过修改模板和视图,实现了专题模块html
原论坛的模式是用户点击节点发帖,而后就归到节点的分类里面了。我须要一个功能,就是右侧须要一个专题区,管理员发帖的话须要显示在那里。为了尽可能小的修改实现功能,我决定设置一个管理员节点,而后在视图调用数据库过滤出节点,就能够了。node
------数据库
那么问题出现了,既然是节点,全部用户均可以点击节点发帖,这岂不乱了?post
因而,我打起了发帖框的主意,若是能使用什么方法,使普通用户点击这个节点时不显示发帖框,管理员点击显示发帖框,从而就实现了对通常用户隐藏,达成目的。因此我翻看了模板。原模板以下:ui
{% if user.is_authenticated %} <div class="box"> <form action="/node/{{node.slug}}/create" method="post" class="form-vertical" onSubmit="return beforeSubmit(this);"> {% csrf_token %} {% for field in form %} <div class="control-group"> <label class="control-label">{{ field.label_tag }}</label> <div class="controls"> {{ field }} </div> <label>{{ field.errors }}</label> </div> {% endfor %} </table> <div class="control-group"> <div class="controls" style="margin-left: 0;float:right;"> <input type="submit" value="建立" class="btn btn-success"> </div> </div> </form> </div> {% else %} <div class="required">您须要 <a style="height: 20px;line-height:20px;" href="/accounts/login">登陆</a> 来发表新话题。没有帐号,立刻 <a style="height: 20px;line-height:20px;" href="/accounts/register">注册</a> 一个。</div> {% endif %}
翻看用户的方法和模板的一些控制语句。找到了user.is_staff和ifnotequal两个颇有用的东东。经过一些逻辑组合,就变成了以下的代码,实现了当用户未登陆时显示请登陆,登录后若是是普通用户,在指定的父节点下,则不显示发帖框,若是是管理员则显示。this
is_staff()能够判断用户是否能够登陆后台spa
{% if user.is_authenticated %} {% if user.is_staff %} <div class="box"> <form action="/node/{{node.slug}}/create" method="post" class="form-vertical" onSubmit="return beforeSubmit(this);"> {% csrf_token %} {% for field in form %} <div class="control-group"> <label class="control-label">{{ field.label_tag }}</label> <div class="controls"> {{ field }} </div> <label>{{ field.errors }}</label> </div> {% endfor %} </table> <div class="control-group"> <div class="controls" style="margin-left: 0;float:right;"> <input type="submit" value="建立" class="btn btn-success"> </div> </div> </form> </div> {% else %} {% ifnotequal node.category.name '父节点名称' %} <div class="box"> <form action="/node/{{node.slug}}/create" method="post" class="form-vertical" onSubmit="return beforeSubmit(this);"> {% csrf_token %} {% for field in form %} <div class="control-group"> <label class="control-label">{{ field.label_tag }}</label> <div class="controls"> {{ field }} </div> <label>{{ field.errors }}</label> </div> {% endfor %} </table> <div class="control-group"> <div class="controls" style="margin-left: 0;float:right;"> <input type="submit" value="建立" class="btn btn-success"> </div> </div> </form> </div> {% endifnotequal %} {% endif %} {% else %} <div class="required">您须要 <a style="height: 20px;line-height:20px;" href="/accounts/login">登陆</a> 来发表新话题。没有帐号,立刻 <a style="height: 20px;line-height:20px;" href="/accounts/register">注册</a> 一个。</div> {% endif %}
这样,templates部分的node.html模板功能就实现了。(这是点击节点后的显示模板)
还有一部分是首页index.html,主界面的部分也须要修改,由于原先有热门话题模块,因此能够很方便的拿来修改。code
{% if special_topics %} <div class="box"> <h4>专题</h4><hr class="line thin"/> {% for item in special_topics %} <div class="cell"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody><tr> <td width="24" valign="middle" align="center"> <a href="/people/{{item.author.get_profile.slug}}" title="{{ item.author.get_profile.name}}"><img src="/media/avatar/{{ item.author.get_profile.avatar }}" class="avatar" border="0" style="max-width: 24px; max-height: 24px;" alt="{{ item.author.get_profile.name}}"></a> </td> <td width="10"></td> <td width="auto" valign="middle"> <span class="hot_topic_title"> <a href="/topic/{{item.id}}" title="{{item.title}}">{{item.title}}</a> </span> </td> </tr> </tbody></table> </div> {% endfor %} <div class="clearfix"></div> </div> {% endif %}
上面本来的hot_topics已经被我替换成special_topics,至于这个special_topics,就须要使用views视图来定义了。
打开site目录的views模块。看见以下的一些代码:orm
hot_topics = Topic.objects.filter(created_on__range=[from_date, time_now]).order_by('-num_replies')[:10]
context['hot_topics'] = hot_topics
hot_nodes = Node.objects.filter(num_topics__gt=0,updated_on__gt=from_date).order_by('-updated_on')[:10]
context['hot_nodes'] = hot_nodes
return render(request,'index.html',context)
这样,经过定义context处理器实现了替换,只须要添加special_topics变量便可。获取指定的节点的数据。在模板中就能够显示了。
在Topic类下有node变量,外键是Node主题帖的节点,Node中有slug属性。这样就能够过滤出节点slug为“zt”的节点的全部主题帖。添加以下代码。csrf
special_topics = Topic.objects.filter(node__slug__iexact="zt").order_by('-num_replies')[:10]
context['special_topics'] = special_topics
OK,使用管理员登录,发帖,刷新,首页出现了“专题”模块。
————————-
吐槽:某度的搜索真心浪费时间,最开始使用node.slug出错,想找找使用外键的例子。就是出不来。
最后使用了基于Google的一个搜索,立刻发现node.slug应该写成node__slug。纠结了好半天!