自定义 django 注释
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的系统正常工作需要满足以下两件事:
标签具有允许文件上传的 enctype 属性,例如
表单已实例化同时使用 request.POST 和 request.FILES,例如
form = form_class(request.POST, request.FILES)
。否则,FileField 将没有任何值。所以您的主题中确实缺少的是:
HTML 表单 和
Python 视图 代码,提示:确保您检查 request.FILES 顺便说一句
让我做出一个也许更具体的答案。
2 things are necessary for your system to work:
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 fileThe 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:
The form HTML and
The view python code, hint: make sure you inspect request.FILES there BTW
For me to make a perhaps more specific answer.