Reputation: 1473
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>
Upvotes: 1
Views: 1444
Reputation: 33420
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 file
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:
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.
Upvotes: 2