Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to upload file with the TypeError : expected str, bytes or os.PathLike object, not list

I have a user_post.html page to let user upload their file(gif, mp4) or image, but when i click confirm i will getting error saying -

expected str, bytes or os.PathLike object, not list.

i have a media folder in my main project folder, inside the media i have another folder call user_post.

inside my setting.py:

MEDIA_URL = 'media'

MEDIA_ROOT = [os.path.join(BASE_DIR,'media')]

did i miss anything or syntax error?

Thank you!

Traceback:

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\GIAM ZENG KEAT\Desktop\cinemagraph_project\cinemagraph_social_project\post\views.py" in Post
  32.             posted.save()

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\base.py" in save
  729.                        force_update=force_update, update_fields=update_fields)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\base.py" in save_base
  759.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\base.py" in _save_table
  842.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\base.py" in _do_insert
  880.                                using=using, raw=raw)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\query.py" in _insert
  1125.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1283.             for sql, params in self.as_sql():

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1236.                 for obj in self.query.objs

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1236.                 for obj in self.query.objs

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
  1235.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py" in pre_save_val
  1185.         return field.pre_save(obj, add=True)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\fields\files.py" in pre_save
  287.             file.save(file.name, file.file, save=False)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\db\models\fields\files.py" in save
  87.         self.name = self.storage.save(name, content, max_length=self.field.max_length)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\files\storage.py" in save
  48.         name = self.get_available_name(name, max_length=max_length)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\files\storage.py" in get_available_name
  72.         while self.exists(name) or (max_length and len(name) > max_length):

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\files\storage.py" in exists
  308.         return os.path.exists(self.path(name))

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\files\storage.py" in path
  321.         return safe_join(self.location, name)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\utils\functional.py" in __get__
  36.         res = instance.__dict__[self.name] = self.func(instance)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\site-packages\django\core\files\storage.py" in location
  201.         return os.path.abspath(self.base_location)

File "C:\Users\GIAM ZENG KEAT\Anaconda3\lib\ntpath.py" in abspath
  542.             path = os.fspath(path)

Exception Type: TypeError at /userpost/post/
Exception Value: expected str, bytes or os.PathLike object, not list

models.py

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import FileExtensionValidator
# Create your models here.

class UserPost(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    cinemagraph = models.FileField(upload_to="user_post/", validators=[FileExtensionValidator(allowed_extensions=['gif','png','jpg'])])
    c_description = models.CharField(max_length=500,default='')
    date_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        # Built-in attribute of django.contrib.auth.models.User !
        return self.user.username

views.py

def Post(request):
if request.method == 'POST':
    form_post = UserPostForm(request.POST, request.FILES)

    if form_post.is_valid():
        posted = form_post.save(commit=False)
        posted.user = request.user
        posted.save()
        return reverse('home')
    else:
        return HttpResponse('Posting failed')
else:
    form_post = UserPostForm()
return render(request,'post/user_post.html',{'form_post':form_post})

forms.py

from django import forms
from django.forms import ModelForm
from post.models import UserPost

class UserPostForm(forms.ModelForm):

    class Meta():
        model = UserPost
        fields= ('cinemagraph','c_description')

user_post.html

{% extends 'base.html'%}
{% load bootstrap3 %}
{% load staticfiles %}

{% block content %}
<div class="container">
  <h1>Post something to our Cinemagraph</h1>
  <form  method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {% bootstrap_form form_post %}
    <input type="submit" class="btn btn-default" value="Confirm">
  </form>
</div>

{% endblock %}
like image 303
giam zengkeat Avatar asked Jul 31 '18 14:07

giam zengkeat


1 Answers

MEDIA_ROOT should be a string not a list.

Change to

MEDIA_ROOT = os.path.join(BASE_DIR,'media')

like image 98
joextodd Avatar answered Sep 28 '22 18:09

joextodd