自定义Django注释

2024-09-29 00:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

Im正在为django构建一个定制的评论应用程序,使用django评论本身。我严格遵守了https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/,我有两个问题,一个是我的自定义注释实例没有给出content_对象。在

所以当我尝试以下方法时,我什么也得不到

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

第二,我的评论并没有把我添加的文件上传到自定义的评论中。在

我想做的另一件事是通过邮件通知,每次有人对某个特定主题发表评论时,都会有一个特定用户的列表,但是我想在通知中添加一个url或评论所发布主题的标题,我该怎么做?在

我的自定义注释模型。在

^{pr2}$

我的自定义模型窗体

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

初始化.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)

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

Tags: djangonameselfdivformfielddataget
1条回答
网友
1楼 · 发布于 2024-09-29 00:18:58

您的系统必须具备两项功能:

  1. 标记有一个enctype属性,允许文件上传,例如<form enctype="multipart/form-data" method="post" action="">,否则浏览器将不发送文件

  2. 窗体与这两个实例关联请求.POST以及请求.FILES,例如form = form_class(request.POST, request.FILES)。否则,FileField将没有任何值。

所以,你的主题缺少的是:

  1. 表单HTML

  2. 查看python代码,提示:确保检查请求.FILES在那里,

让我做一个更具体的回答。在

相关问题 更多 >