自定义 django 注释

发布于 2025-01-08 02:40:04 字数 3528 浏览 0 评论 0原文

我使用 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>

Im building a customized comments app for django, using django comments itself. Ive followed https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/ to the letter, and I have 2 issues, one is that my custom comments instances do not give content_object.

So when I try the following I get nothing

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

And two, my comments are not taking the uploading the files I add within the form of the customized comments.

Another thing that I would like to do is to notify by mail, a list of specific users every time someone comments on an specific topic, but I would like to add to the notification a url or the title of the topic the comment was posted on, how could I do this?

My custom comment model.

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

My custom model form

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

And in the init.py

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

def get_model():
    return CommentWithFile

def get_form():
    return CommentFormWithFile

The admin file for my commentwithfile

from apps.comments.models import CommentWithFile

class CommentWithFileAdmin(admin.ModelAdmin):
    pass

admin.site.register(CommentWithFile, CommentWithFileAdmin)

Im using django 1.3.1, and have django notifications in order to notify user of comments.

Thank you everyone!

==== UPDATE ====

Here is the comment form template

{% 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>

An heres how I render this form in other templates

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-15 02:40:04

您的系统正常工作需要满足以下两件事:

  1. 标签具有允许文件上传的 enctype 属性,例如

    ,否则浏览器将不会发送文件

  2. 表单已实例化同时使用 request.POST 和 request.FILES,例如 form = form_class(request.POST, request.FILES)。否则,FileField 将没有任何值。

所以您的主题中确实缺少的是:

  1. HTML 表单

  2. Python 视图 代码,提示:确保您检查 request.FILES 顺便说一句

让我做出一个也许更具体的答案。

2 things are necessary for your system to work:

  1. The tag has an enctype attribute allowing file uploads, e.g. <form enctype="multipart/form-data" method="post" action="">, or the browser will not send the file

  2. The form is instanciated with both request.POST and request.FILES, e.g. form = form_class(request.POST, request.FILES). Else the FileField will not have any value.

So really what's missing from your topic are:

  1. The form HTML and

  2. The view python code, hint: make sure you inspect request.FILES there BTW

For me to make a perhaps more specific answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文