Django Comments: Want to remove user URL, not expand the model. How to?

前端 未结 3 860
礼貌的吻别
礼貌的吻别 2020-12-31 08:36

I\'m totally understanding the documentation on expanding the Comments app in Django, and really would like to stick with the automatic functionality but...

相关标签:
3条回答
  • 2020-12-31 08:44

    My quick and dirty solution: I made the the 'email' and 'url' fields hidden fields, with an arbitrary value to get rid of 'this field is required' errors.

    It's not elegant, but it's quick and I didn't have to subclass CommentForm. All the work of adding comments was done in the template, which is nice. It looks like this (warning: not tested, since it's a simplified version of my actual code):

    {% 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="foo@foo.foo" />
        <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>
    
    0 讨论(0)
  • 2020-12-31 08:51

    I can't comment onto SmileyChris' post for some reason, so I am going to post it here. But, I ran into errors using just SmileyChris' response. You also have to overwrite the get_comment_create_data function because CommentForm is going to look for those Post keys that you removed. So here's my code after I removed three fields.

    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')
    

    This is the function that you are overwriting

    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,
        )
    
    0 讨论(0)
  • 2020-12-31 09:04

    This is well documented under customizing the comments framework.

    All your app will use is get_form, returning a subclass of the CommentForm with the url field popped. Something like:

    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')
    
    0 讨论(0)
提交回复
热议问题