爲Pythonic論壇添加一個「專題」功能

代碼還沒讀完就踏上了修改功能的深坑。還好思路清晰,經過修改模板和視圖,實現了專題模塊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_staffifnotequal兩個頗有用的東東。經過一些邏輯組合,就變成了以下的代碼,實現了當用戶未登陸時顯示請登陸,登錄後若是是普通用戶,在指定的父節點下,則不顯示發帖框,若是是管理員則顯示。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。糾結了好半天!

相關文章
相關標籤/搜索