CreateView - 尝试保存图像上传时出现 TypeError
CreateView - Getting a TypeError when trying to save an image upload
我正在努力思考图像和 Form handling with class-based views。所以我正在玩这个简单的模型。使用 CreateView 时,我是否必须使用 form_class 用自定义表单覆盖它才能成功 upload/save 和图像?
我不明白为什么我会收到这个:
Exception Type: TypeError
Exception Value: expected str, bytes or os.PathLike object, not tuple
整件都是准系统,缺货。
models.py
class Post(models.Model):
headline = models.CharField(max_length=80, blank=True, null=True)
cover_image = models.ImageField('hero image', upload_to='images/', blank=True, null=True)
slug = models.SlugField(max_length=80, blank=True, null=True, unique=True)
def get_absolute_url(self):
return reverse('details', args=[str(self.slug)])
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['headline', 'cover_image', 'slug']
news/templates/news/post_form.py
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save"/>
</form>
谁能帮我理解一下?
根据要求
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = (os.path.join(BASE_DIR, 'media'),)
MEDIA_URL = '/media/'
LOGIN_REDIRECT_URL = 'home'
您已将 MEDIA_ROOT 设置为一个元组,但它应该是一个字符串。
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
我正在努力思考图像和 Form handling with class-based views。所以我正在玩这个简单的模型。使用 CreateView 时,我是否必须使用 form_class 用自定义表单覆盖它才能成功 upload/save 和图像?
我不明白为什么我会收到这个:
Exception Type: TypeError
Exception Value: expected str, bytes or os.PathLike object, not tuple
整件都是准系统,缺货。
models.py
class Post(models.Model):
headline = models.CharField(max_length=80, blank=True, null=True)
cover_image = models.ImageField('hero image', upload_to='images/', blank=True, null=True)
slug = models.SlugField(max_length=80, blank=True, null=True, unique=True)
def get_absolute_url(self):
return reverse('details', args=[str(self.slug)])
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['headline', 'cover_image', 'slug']
news/templates/news/post_form.py
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save"/>
</form>
谁能帮我理解一下?
根据要求
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = (os.path.join(BASE_DIR, 'media'),)
MEDIA_URL = '/media/'
LOGIN_REDIRECT_URL = 'home'
您已将 MEDIA_ROOT 设置为一个元组,但它应该是一个字符串。
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')