helloflask / flask-ckeditor

CKEditor integration for Flask, including image upload, code syntax highlight, and more.
https://flask-ckeditor.readthedocs.io
MIT License
197 stars 67 forks source link

AttributeError: 'NoneType' object has no attribute 'filename' #52

Closed Maxino22 closed 3 years ago

Maxino22 commented 3 years ago

I have set up everything as per documentation and example but i am getting this error. below is my route that handles the image upload the jinja error is on the f.filename.split. Kindly assist

@port.route('/admin/add', methods=['GET', 'POST'])
@login_required
def add_post():
    categories = Category.query.all()

    blog = Blog()

    f = request.files.get('upload')

    extension = f.filename.split('.')[-1].lower()

    if extension not in ['jpg', 'gif', 'png', 'jpeg']:
        return upload_fail(message='Image only!')
    f.save(os.path.join(app.config['UPLOADED_PATH'], f.filename))
    url = url_for('uploaded_files', filename=f.filename)
    return upload_success(url=url)

    # form
    form = ArticleForm()

    if form.validate_on_submit():
        image_url = photos.url(photos.save(form.picture.data))

        new_blog = Blog(title=form.title.data, category_id=int(request.form['category_id']),
                        picture=image_url, user_id=current_user.id, text=form.text.data)
        db.session.add(new_blog)
        db.session.commit()
        flash("Blog Posted", 'success')
        return redirect(url_for('port.admin'))

    return render_template('admin/add_post.html', form=form, categories=categories, blog=blog)
remyzandwijk commented 3 years ago

This is more a general Flask question then a specific question about Flask-CKeditor. So you might want to ask your question in a dedicated Flask forum.

However, we can take a look of course, but we need more information. Could you share the template you are using and the code that builds the form?

Maxino22 commented 3 years ago

Thanks for response , This is the template for my form

{% extends 'admin/base.html' %}

{% block title %}

Add Post
{% endblock  %}
 {% block content %}
 <div class="container">
   <div class="row">
     <div class="col-lg-12  mt-4">

              <form method="POST" enctype ="multipart/form-data">
                {{form.csrf_token}}
                <div class="form-group">
                  <label for="title">Title</label>
                  {{form.title(class="form-control")}}
                </div>
                <div class="form-group">
                  <label for="category">Category</label>
                  <select name="category_id" class="form-control">
                    {% for category in categories %}
                    <option value={{category.id}}>{{category.name}}</option>
                    {% endfor %}
                  </select>
                </div>
                <div class="form-group">
                  <label for="file">Image Upload</label>
                  {{form.picture(class="form-control-file")}}
                  <small class="form-text text-muted">Max Size 3mb</small>
                </div>
                <div class="form-group">
                  <label for="body">Body</label>
                  {{form.text(class="form-control")}}
                </div>
                  {{form.submit(class="btn btn-primary")}}
                </form>
                {{ckeditor.load(custom_url=url_for('static', filename='ckeditor/ckeditor.js'))}}
                {{ckeditor.config(name="text")}}
              </div> 
     </div>
   </div>

 {% endblock  %}
greyli commented 3 years ago

Please provide the full error traceback (whenever you ask a question about an error, be sure always to include the full error traceback).

greyli commented 3 years ago

The screenshot is not helpful, and it's not the full error traceback, you could copy the plaintext of error trackback from your terminal.

Maxino22 commented 3 years ago

I have used flask uploads and found away around it using only the image link thank you for your time and interest to help me