Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django crispy forms file upload

I'm having trouble getting an image field to upload correctly with django crispy forms. Everything else on the form and page works fine, but the image field does not error or save, it just passes right over it. I am able to add the image through admin just fine, it only fails on my crispy form.

#views.py
@login_required
def dashboard(request, **kwargs):
    '''
    perform managerial and administrative tasks for a blog. Only
    available to the blog owner
    '''
    alert_message = ""
    status = ""

    blog_slug = kwargs['blog']
    blog = get_object_or_404(PersonalBlog, slug=blog_slug)

    # handle the form submit to update blog data
    form = BlogEditForm(instance=blog)
    if request.method == "POST":
        if blog.owner == request.user:
            form = BlogEditForm(request.POST, instance=blog)

            if form.is_valid():
                form.save()

                alert_message = "Your blog data has been updated successfully"
                status = "saved"
            else:
                alert_message = "There was a problem saving the data you entered. Please correct the errors above."
                status = "error"
        else:
            alert_message = "You do not have access to update this blog's information."
            status = "error"


    return render(request, "blogcontent/dashboard.html", {'alert_message':alert_message,
                                                          'status':status, 'form':form})


#forms.py
class BlogEditForm(ModelForm):
    description = forms.CharField(widget = forms.Textarea())
    twitter = forms.CharField(required=False, help_text="show twitter feed, and allow people to interact with you on twitter")
    twitter_widget_id = forms.CharField(required=False, help_text="required to show a timeline widget for your twitter account. " + \
                                                  "<span class='glyphicon glyphicon-question-sign'></span>")
    instagram = forms.CharField(required=False, help_text="show instagram feed on your blog page")
    disqus = forms.CharField(required=False, help_text="allow comments at the bottom of your blog posts")

    class Meta:
        model = PersonalBlog

    def __init__(self, *args, **kwargs):
        super(BlogEditForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                '<h2 class="text-center">Edit data about your blog</h2>',
                Field('owner', type="hidden"),
                Field('title', type="hidden"),
                Div('description', css_class="col-md-12"),
                Div('twitter', css_class="col-md-6"),
                Div('twitter_widget_id', css_class="col-md-6"),
                Div('instagram', css_class="col-md-6"),
                Div('disqus', css_class="col-md-6"),
                Div('logo', css_class="col-md-12"),
            ),
            ButtonHolder(
                Submit('submit', 'Update Info', css_class='btn-lg'),
                css_class="text-center"
            ),

        )

#dashboard.html
<form method="post" enctype="multipart/form-data">
                        {% crispy form %}
                    </form>
like image 585
awwester Avatar asked May 14 '26 17:05

awwester


2 Answers

Ah, I knew it was going to be something dumb. I was not attaching the request.FILES to the form.save() object.

form = BlogEditForm(request.POST, request.FILES, instance=blog)
like image 89
awwester Avatar answered May 17 '26 09:05

awwester


Have you tried changing:

<form method="post" enctype="multipart/form-data">
    {% crispy form %}
</form>

To just:

{% load crispy_forms_tags %}
{% crispy form %}

Also in your code, your are calling:

if form.is_valid():
    form.save()

You don't want to save() the form. You want to save the form data to a model instance. So create() a model using the form data. This may be why as well. Forms are just used to hold data until you clean it and save it to the database using a model.

like image 41
Aaron Lelevier Avatar answered May 17 '26 07:05

Aaron Lelevier