自定义django评论

时间:2012-02-20 17:00:17

标签: python django django-models django-forms django-comments

我正在为django构建一个自定义评论应用程序,使用django评论本身。我跟着https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/跟着这封信,我有两个问题,一个是我的自定义评论实例没有给出content_object。

因此,当我尝试以下内容时,我什么都没得到

c = CommentWithFile.object.get(id)=1 
c.content_object

其中两个,我的评论不是上传我在自定义评论表单中添加的文件。

我想做的另一件事是每次有人对特定主题发表评论时通过邮件通知特定用户的列表,但我想在通知中添加一个网址或主题的标题评论发布了,我怎么能这样做?

我的自定义评论模型。

def upload_path(instance, filename):
    return '/'.join(['uploads','comment_archives', filename])

class CommentWithFile(Comment):

    comment_file = models.FileField(max_length=255, upload_to=upload_path, 
        blank=True, null=True)
    notify = models.BooleanField(_("Notificar usuarios"))

    @property
    def fileobject(self):
        if self.comment_file:
            return FileObject(self.comment_file.path)
        return None

我的自定义模型表单

class CommentFormWithFile(CommentForm):
    comment_file = forms.FileField(label=_("Archivo"), required=False)
    notify = form.BooleanField(label=_("Notificar usuarios"))

    def get_comment_model(self):
        # Use our custom comment model instead of the built-in one.
        return CommentWithFile

    def get_comment_create_data(self):
        # Use the data of the superclass, and add in the title field
        data = super(CommentFormWithFile, self).get_comment_create_data()
        data['comment_file'] = self.cleaned_data['comment_file']
        data['notify'] = self.cleaned_data['notify']
        return data

init .py

from apps.comments.models import CommentWithFile
from apps.comments.forms import CommentFormWithFile

def get_model():
    return CommentWithFile

def get_form():
    return CommentFormWithFile

我的commentwithfile的管理文件

from apps.comments.models import CommentWithFile

class CommentWithFileAdmin(admin.ModelAdmin):
    pass

admin.site.register(CommentWithFile, CommentWithFileAdmin)

我使用django 1.3.1,并有django通知,以通知用户评论。

谢谢大家!

====更新====

这是评论表单模板

{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
  {% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
  {% for field in form %}
    {% if field.is_hidden %}
      <div>{{ field }}</div>
    {% else %}
      {% if field.errors %}{{ field.errors }}{% endif %}
      <p
        {% if field.errors %} class="error"{% endif %}
        {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
        {% if field.label == 'Comentario' or field.label == 'Archivo' %}
        {{ field }}
        {% endif %}
      </p>
    {% endif %}
  {% endfor %}

  <div class="actions">
    <input type="hidden" name="next" value="{{ request.path }}" />
    <input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
    <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" />
  </div>
</form>

如何在其他模板中呈现此表单

    {% get_comment_form for archive as form %}
    <h4>Comentar</h4>

    <div class="main_comment" id="comment_form">
        {% render_comment_form for archive %}
    </div>

1 个答案:

答案 0 :(得分:2)

系统运行需要2件事情:

  1. 标签具有允许文件上传的enctype属性,例如<form enctype="multipart/form-data" method="post" action="">,或浏览器发送文件

  2. 表单与request.POST和request.FILES 一起实例化,例如form = form_class(request.POST, request.FILES)。否则FileField将没有任何价值。

  3. 所以你的主题中真正缺少的是:

    1. 表单HTML

    2. 视图python 代码,提示:请务必检查request.FILES那里BTW

    3. 让我做出更具体的答案。