2009-09-21 37 views
12

我完全理解在Django的擴大應用註釋的文檔,真正想堅持使用自動功能 ...Django評論:想要刪除用戶的URL,而不是擴大模型。如何?

在當前的應用程序,我絕對沒有使用一個「 URL「與評論一起提交。

作爲默認設置的微創我怎麼能阻止這個領域進行展示與評論表單嗎?

使用Django 1,或樹幹,和許多普通/內置插件越好(一般的看法,默認設置註釋了,等我只有一個通用視圖包裝至今)。

回答

9

customizing the comments framework下有據可查。

您的所有應用程序將使用的是get_form,返回CommentForm的子類,並彈出url字段。喜歡的東西:

class NoURLCommentForm(CommentForm): 
    """ 
    A comment form which matches the default djanago.contrib.comments one, but 
    doesn't have a URL field. 

    """ 
NoURLCommentForm.base_fields.pop('url') 
+7

我同意的文件是偉大的,但我不得不認爲,找到我的方式「base_fields」和使用。 pop()不是我隨便碰到的東西。是的,評論框架是有據可查的,但「這個」不是。我同意我有責任去找到這樣的事情,我非常感謝您的時間和幫助!多謝兄弟! – 2009-09-22 01:01:21

+0

這會在Django 1.4中引發錯誤。您還需要覆蓋killerbarney解決方案中提到的get_comment_create_data方法。 – 2013-02-14 00:37:10

16

我不能到SmileyChris'後由於某種原因發表評論,所以我將它張貼在這裏。但是,我只用SmileyChris的回答就遇到了錯誤。您還必須覆蓋get_comment_create_data函數,因爲CommentForm將查找您刪除的那些Post鍵。所以這是我刪除三個字段後的代碼。

class SlimCommentForm(CommentForm): 
""" 
A comment form which matches the default djanago.contrib.comments one, but with 3 removed fields 
""" 
def get_comment_create_data(self): 
    # Use the data of the superclass, and remove extra fields 
    return dict(
     content_type = ContentType.objects.get_for_model(self.target_object), 
     object_pk = force_unicode(self.target_object._get_pk_val()), 
     comment  = self.cleaned_data["comment"], 
     submit_date = datetime.datetime.now(), 
     site_id  = settings.SITE_ID, 
     is_public = True, 
     is_removed = False, 
    ) 


SlimCommentForm.base_fields.pop('url') 
SlimCommentForm.base_fields.pop('email') 
SlimCommentForm.base_fields.pop('name') 

這是將要覆蓋

def get_comment_create_data(self): 
    """ 
    Returns the dict of data to be used to create a comment. Subclasses in 
    custom comment apps that override get_comment_model can override this 
    method to add extra fields onto a custom comment model. 
    """ 
    return dict(
     content_type = ContentType.objects.get_for_model(self.target_object), 
     object_pk = force_unicode(self.target_object._get_pk_val()), 
     user_name = self.cleaned_data["name"], 
     user_email = self.cleaned_data["email"], 
     user_url  = self.cleaned_data["url"], 
     comment  = self.cleaned_data["comment"], 
     submit_date = datetime.datetime.now(), 
     site_id  = settings.SITE_ID, 
     is_public = True, 
     is_removed = False, 
    ) 
+5

你需要這些import語句:從django.contrib.contenttypes.models進口的ContentType 從django.conf導入設置 進口日期時間 – PhoebeB 2011-07-26 12:50:10

+0

根據意見框架django.utils.encoding進口force_unicode 正在擴展,'Django的。 contrib.comments'或'django_comments'(I收集後者是較新的),'從django_comments.forms導入CommentForm' – jozxyqk 2014-09-02 14:35:08

5

我的快速和骯髒的解決方案的功能:我做了「電子郵件」和「URL」字段隱藏字段,爲任意值擺脫'這個字段是必需的'錯誤。

這不是優雅,但它的快速和我沒有子類CommentForm。所有添加評論的工作都是在模板中完成的,這很好。它看起來像這樣(警告:沒有測試,因爲這是我的實際代碼的簡化版本):

{% get_comment_form for entry as form %} 

<form action="{% comment_form_target %}" method="post"> {% csrf_token %} 

{% for field in form %} 

    {% if field.name != 'email' and field.name != 'url' %} 
     <p> {{field.label}} {{field}} </p> 
    {% endif %} 

{% endfor %} 

    <input type="hidden" name="email" value="[email protected]" /> 
    <input type="hidden" name="url" value="http://www.foofoo.com" /> 

    <input type="hidden" name="next" value='{{BASE_URL}}thanks_for_your_comment/' /> 
    <input type="submit" name="post" class="submit-post" value="Post"> 
</form>