python - django custom comment's image field not working -
i'm doing django-contrib-comment's custom comment app django app. @ long lastly achieved comment app has image field, , shows in template of course. here thing, when i'm trying post comment image, not saving image file, , says empty(this field required). can help me figure out. here code snippets.
models.py
class commentwithpic(comment): image = models.imagefield(upload_to="comments/%y/%m/%d/", null=true, blank=true)
forms.py
class commentformwithpic(commentform): image = forms.imagefield() def get_comment_model(self): homecoming commentwithpic def get_comment_create_data(self): info = super(commentformwithpic, self).get_comment_create_data() data['image'] = self.cleaned_data['image'] homecoming info
post_with_comment.html
{% render_comment_list adi %} {% get_comment_form adi form %} <form action="{% comment_form_target %}" method="post"> {% csrf_token %} {% field in form %} {% if field.is_hidden %} {{ field }} {% else %} {% if field.errors %}{{ field.errors }}{% endif %} {{ field.label }} {{ field }} {% endif %} {% endfor %} <input type="hidden" value="{% url 'ad' adi.id %}"/> <input type="submit" value="comment"/> </form>
you need declare form next (notice enctype
attribute) when dealing `imagefield
<form enctype="multipart/form-data" action="{% comment_form_target %}" method="post">\
to bind uploads form:
dealing forms have filefield , imagefield fields little more complicated normal form. firstly, in order upload files, you’ll need create sure element correctly defines enctype "multipart/form-data"
you need create image optional in form:
class commentformwithpic(commentform): image = forms.imagefield(required=false) ...
python django image comments
No comments:
Post a Comment