2009-12-07 119 views
0

所以,我使用django.contrib.comments。我已經安裝了它,但不是笨重的默認評論表單,我想使用一個只顯示textarea和提交按鈕的自定義表單模板。定製django評論

這背後的基本原理是,用戶只能看到表單,如果他們的區域已經過身份驗證,我想保持簡單的表單,並自動拿起他們的用戶名等。

我已經實現了一個自定義表單,但是當我嘗試提交時出現錯誤。

下面是我在我與評論表單的頁面模板(條目從視圖中傳遞的對象):

{% load comments %} 
{% render_comment_form for entry %} 

這是我的HTML中/templates/comments/form.html:

{% if user.is_authenticated %} 
    <p>Submit a comment:</p> 
    <form action="/comments/post/" method="post"> 
    <textarea name="comment" id="id_comment" rows="2" style="width: 90%;"></textarea> 
      <input type="hidden" name="options" value="{{ options }}" /> 
      <input type="hidden" name="target" value="{{ target }}" /> 
      <input type="hidden" name="gonzo" value="{{ hash }}" /> 
      <input type="hidden" name="next" value="{{ entry.get_absolute_url }}" /> 
      <span style="float:right;"><input type="submit" name="post" value="Add"></span> 
    </form> 
    {% else %} 
     <p>Please <a href="/login/">log in</a> to post a comment.</p> 
    {% endif %} 

它呈現好的開始,但是當我嘗試提交評論的形式,我得到以下的Django錯誤:

Comment post not allowed (400) 
Why: Missing content_type or object_pk field. 

任何人都可以幫忙嗎?

回答

1

Theju's app複製固定的問題 - 尤其是看到在第2部分

約書亞作品評論
1

評論模型使用通用外鍵映射到進行評論的對象,如博客條目。這些是標準評論表單中所需的隱藏字段。

從django.contrib.comments.models

... 
class CommentSecurityForm(forms.Form): 
    """ 
    Handles the security aspects (anti-spoofing) for comment forms. 
    """ 
    content_type = forms.CharField(widget=forms.HiddenInput) 
    object_pk  = forms.CharField(widget=forms.HiddenInput) 
... 

如果你沒有改變窗體類,並只希望更改HTML模板,那麼你可以通過在所有加入的循環包括這些領域隱藏的領域。

{% for hidden in form.hidden_fields %} 
    {{ hidden }} 
{% endfor %}