librelist archives

« back to archive

Problems uploading image during edits

Problems uploading image during edits

From:
Madireddy Samuel Vijaykumar
Date:
2012-06-27 @ 05:11
Hi.

I am unsure if this is the right forum. But since I am using flask as the
base framework, i thought this is right.

I have an application that have has form which has an images upload option.
There are two such forms on is the Create Record form and other is Edit
Record form. I am using flask-wtforms for defining my forms. When I upload
using the Create Record form I have no problems, the form just successfully
submits. But when I try to edit a record and try and upload the an images.
I get the following error

Traceback (most recent call last):
  File "/livingcode/sandbox/17z/qrzbootstrap/qruize/views.py", line 168, in
editcampaign
    mascot_filename = "%s_%s"%
(image_prefix(),secure_filename(form.mascot.data.filename))
AttributeError: 'unicode' object has no attribute 'filename'

This is my code for the Create Record that works fine....

def newcampaign():
    next_url = request.args.get('next') or url_for('index')
    form = CampaignProfile(enctype="multipart/form-data")
    if form.validate_on_submit():
        campaign = QruizeCampaign()
        form.populate_obj(campaign)
        mascot_filename = ''

        if form.mascot.data:
            try:
                print form.mascot.data
                mascot_filename = "%s_%s"%
(image_prefix(),secure_filename(form.mascot.data.filename))
                logos.save(form.mascot.data, name=mascot_filename)
            except:
                print_exc()
                flash("Not a valid image, check type and size",
category="error")

                return render_template('newcampaign.html', form_title='New
Qruize Campaign',
                           form=form, formid='campaign_new',
enctype="multipart/form-data",
                           action='/campaign/new', submit='Save Campaign')

        campaign.logo = mascot_filename
        db.session.add(campaign)
        db.session.commit()
        return redirect(url_for('campaigns'))

    return render_template('newcampaign.html', form_title='New Qruize
Campaign',
                           form=form, formid='campaign_new',
enctype="multipart/form-data",
                           action='/campaign/new', submit='Save Campaign')


while here is my Edit Record

def editcampaign(campaign_id=None):
    campaign =
QruizeCampaign.query.filter_by(id=campaign_id).filter_by(user_id=g.user.id
).first()
    form = CampaignProfile(obj=campaign, enctype="multipart/form-data")
    form.edit_obj = campaign
    if form.validate_on_submit():
        form.populate_obj(campaign)
        mascot_filename = ''

        if form.mascot.data:
            try:
                print form.mascot.data
                mascot_filename = "%s_%s"%
(image_prefix(),secure_filename(form.mascot.data.filename))
                logos.save(form.mascot.data, name=mascot_filename)
                campaign.logo = mascot_filename
            except:
                print_exc()
                flash("Not a valid image, check type and size",
category="error")

                return render_template('editcampaign.html', form_title='New
Qruize Campaign',
                           form=form, formid='campaign_new',
enctype="multipart/form-data",
                           action='/campaign/edit/%s'% campaign_id,
submit='Save Campaign')

        db.session.commit()

        next_url = request.args.get('next') or url_for('campaigns')
        return redirect(next_url)
    action = '/campaign/edit/%s'% campaign_id
    return render_template('editcampaign.html', form_title='Edit Campaign',
                            form=form, formid='campaign_edit',
                            action=action, submit='Save Campaign')

-----

Samuel Vijaykumar M, http://samof76.me (mad.vijay@gmail.com,
samof76@gmail.com)

A3 Valathi Flats. 24 Dr Thomas Road. T Nagar. Chennai 600 017


*All hard work brings a profit, but mere talk leads only to poverty.
-- Proverbs 14:23*

-----

Re: [flask] Problems uploading image during edits

From:
Anthony Ford
Date:
2012-06-28 @ 08:17
Make sure your form has `enctype="multipart/form-data"`  within the opening
form tags.

Last time I forgot that, I got the same AttributeError.


Anthony Ford,
KF5IBN,
Research Assistant,
Center for Advanced Radio Astronomy,
University of Texas at Brownsville
ford.anthonyj@gmail.com



On Wed, Jun 27, 2012 at 1:11 AM, Madireddy Samuel Vijaykumar <
mad.vijay@gmail.com> wrote:

> Hi.
>
> I am unsure if this is the right forum. But since I am using flask as the
> base framework, i thought this is right.
>
> I have an application that have has form which has an images upload
> option. There are two such forms on is the Create Record form and other is
> Edit Record form. I am using flask-wtforms for defining my forms. When I
> upload using the Create Record form I have no problems, the form just
> successfully submits. But when I try to edit a record and try and upload
> the an images. I get the following error
>
> Traceback (most recent call last):
>   File "/livingcode/sandbox/17z/qrzbootstrap/qruize/views.py", line 168,
> in editcampaign
>     mascot_filename = "%s_%s"%
> (image_prefix(),secure_filename(form.mascot.data.filename))
> AttributeError: 'unicode' object has no attribute 'filename'
>
> This is my code for the Create Record that works fine....
>
> def newcampaign():
>     next_url = request.args.get('next') or url_for('index')
>     form = CampaignProfile(enctype="multipart/form-data")
>     if form.validate_on_submit():
>         campaign = QruizeCampaign()
>         form.populate_obj(campaign)
>         mascot_filename = ''
>
>         if form.mascot.data:
>             try:
>                 print form.mascot.data
>                 mascot_filename = "%s_%s"%
> (image_prefix(),secure_filename(form.mascot.data.filename))
>                 logos.save(form.mascot.data, name=mascot_filename)
>             except:
>                 print_exc()
>                 flash("Not a valid image, check type and size",
> category="error")
>
>                 return render_template('newcampaign.html', form_title='New
> Qruize Campaign',
>                            form=form, formid='campaign_new',
> enctype="multipart/form-data",
>                            action='/campaign/new', submit='Save Campaign')
>
>         campaign.logo = mascot_filename
>         db.session.add(campaign)
>         db.session.commit()
>         return redirect(url_for('campaigns'))
>
>     return render_template('newcampaign.html', form_title='New Qruize
> Campaign',
>                            form=form, formid='campaign_new',
> enctype="multipart/form-data",
>                            action='/campaign/new', submit='Save Campaign')
>
>
> while here is my Edit Record
>
> def editcampaign(campaign_id=None):
>     campaign =
> QruizeCampaign.query.filter_by(id=campaign_id).filter_by(user_id=g.user.id
> ).first()
>     form = CampaignProfile(obj=campaign, enctype="multipart/form-data")
>     form.edit_obj = campaign
>     if form.validate_on_submit():
>         form.populate_obj(campaign)
>         mascot_filename = ''
>
>         if form.mascot.data:
>             try:
>                 print form.mascot.data
>                 mascot_filename = "%s_%s"%
> (image_prefix(),secure_filename(form.mascot.data.filename))
>                 logos.save(form.mascot.data, name=mascot_filename)
>                 campaign.logo = mascot_filename
>             except:
>                 print_exc()
>                 flash("Not a valid image, check type and size",
> category="error")
>
>                 return render_template('editcampaign.html',
> form_title='New Qruize Campaign',
>                            form=form, formid='campaign_new',
> enctype="multipart/form-data",
>                            action='/campaign/edit/%s'% campaign_id,
> submit='Save Campaign')
>
>         db.session.commit()
>
>         next_url = request.args.get('next') or url_for('campaigns')
>         return redirect(next_url)
>     action = '/campaign/edit/%s'% campaign_id
>     return render_template('editcampaign.html', form_title='Edit Campaign',
>                             form=form, formid='campaign_edit',
>                             action=action, submit='Save Campaign')
>
> -----
>
> Samuel Vijaykumar M, http://samof76.me (mad.vijay@gmail.com,
> samof76@gmail.com)
>
> A3 Valathi Flats. 24 Dr Thomas Road. T Nagar. Chennai 600 017
>
>
> *All hard work brings a profit, but mere talk leads only to poverty.
> -- Proverbs 14:23*
>
> -----
>
>

Re: [flask] Problems uploading image during edits

From:
Madireddy Samuel Vijaykumar
Date:
2012-06-28 @ 10:51
Great, thanks. It works now.

Sam

-----

Samuel Vijaykumar M, http://samof76.me (mad.vijay@gmail.com,
samof76@gmail.com)

A3 Valathi Flats. 24 Dr Thomas Road. T Nagar. Chennai 600 017


*All hard work brings a profit, but mere talk leads only to poverty.
-- Proverbs 14:23*

-----



On Thu, Jun 28, 2012 at 1:47 PM, Anthony Ford <ford.anthonyj@gmail.com>wrote:

> Make sure your form has `enctype="multipart/form-data"`  within the
> opening form tags.
>
> Last time I forgot that, I got the same AttributeError.
>
>
> Anthony Ford,
> KF5IBN,
> Research Assistant,
> Center for Advanced Radio Astronomy,
> University of Texas at Brownsville
> ford.anthonyj@gmail.com
>
>
>
> On Wed, Jun 27, 2012 at 1:11 AM, Madireddy Samuel Vijaykumar <
> mad.vijay@gmail.com> wrote:
>
>> Hi.
>>
>> I am unsure if this is the right forum. But since I am using flask as the
>> base framework, i thought this is right.
>>
>> I have an application that have has form which has an images upload
>> option. There are two such forms on is the Create Record form and other is
>> Edit Record form. I am using flask-wtforms for defining my forms. When I
>> upload using the Create Record form I have no problems, the form just
>> successfully submits. But when I try to edit a record and try and upload
>> the an images. I get the following error
>>
>> Traceback (most recent call last):
>>   File "/livingcode/sandbox/17z/qrzbootstrap/qruize/views.py", line 168,
>> in editcampaign
>>     mascot_filename = "%s_%s"%
>> (image_prefix(),secure_filename(form.mascot.data.filename))
>> AttributeError: 'unicode' object has no attribute 'filename'
>>
>> This is my code for the Create Record that works fine....
>>
>> def newcampaign():
>>     next_url = request.args.get('next') or url_for('index')
>>     form = CampaignProfile(enctype="multipart/form-data")
>>     if form.validate_on_submit():
>>         campaign = QruizeCampaign()
>>         form.populate_obj(campaign)
>>         mascot_filename = ''
>>
>>         if form.mascot.data:
>>             try:
>>                 print form.mascot.data
>>                 mascot_filename = "%s_%s"%
>> (image_prefix(),secure_filename(form.mascot.data.filename))
>>                 logos.save(form.mascot.data, name=mascot_filename)
>>             except:
>>                 print_exc()
>>                 flash("Not a valid image, check type and size",
>> category="error")
>>
>>                 return render_template('newcampaign.html',
>> form_title='New Qruize Campaign',
>>                            form=form, formid='campaign_new',
>> enctype="multipart/form-data",
>>                            action='/campaign/new', submit='Save Campaign')
>>
>>         campaign.logo = mascot_filename
>>         db.session.add(campaign)
>>         db.session.commit()
>>         return redirect(url_for('campaigns'))
>>
>>     return render_template('newcampaign.html', form_title='New Qruize
>> Campaign',
>>                            form=form, formid='campaign_new',
>> enctype="multipart/form-data",
>>                            action='/campaign/new', submit='Save Campaign')
>>
>>
>> while here is my Edit Record
>>
>> def editcampaign(campaign_id=None):
>>     campaign =
>> QruizeCampaign.query.filter_by(id=campaign_id).filter_by(user_id=
>> g.user.id).first()
>>     form = CampaignProfile(obj=campaign, enctype="multipart/form-data")
>>     form.edit_obj = campaign
>>     if form.validate_on_submit():
>>         form.populate_obj(campaign)
>>         mascot_filename = ''
>>
>>         if form.mascot.data:
>>             try:
>>                 print form.mascot.data
>>                 mascot_filename = "%s_%s"%
>> (image_prefix(),secure_filename(form.mascot.data.filename))
>>                 logos.save(form.mascot.data, name=mascot_filename)
>>                 campaign.logo = mascot_filename
>>             except:
>>                 print_exc()
>>                 flash("Not a valid image, check type and size",
>> category="error")
>>
>>                 return render_template('editcampaign.html',
>> form_title='New Qruize Campaign',
>>                            form=form, formid='campaign_new',
>> enctype="multipart/form-data",
>>                            action='/campaign/edit/%s'% campaign_id,
>> submit='Save Campaign')
>>
>>         db.session.commit()
>>
>>         next_url = request.args.get('next') or url_for('campaigns')
>>         return redirect(next_url)
>>     action = '/campaign/edit/%s'% campaign_id
>>     return render_template('editcampaign.html', form_title='Edit
>> Campaign',
>>                             form=form, formid='campaign_edit',
>>                             action=action, submit='Save Campaign')
>>
>> -----
>>
>> Samuel Vijaykumar M, http://samof76.me (mad.vijay@gmail.com,
>> samof76@gmail.com)
>>
>> A3 Valathi Flats. 24 Dr Thomas Road. T Nagar. Chennai 600 017
>>
>>
>> *All hard work brings a profit, but mere talk leads only to poverty.
>> -- Proverbs 14:23*
>>
>> -----
>>
>>
>