CreateView在尝试保存图像上载时遇到类型错误

2024-04-26 23:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着把我的脑袋放在图像和Form handling with class-based views上。所以我在玩这个简单的模型。当使用CreateView时,我是否必须使用form\u类用自定义表单覆盖它才能成功上载/保存图像?你知道吗

我不明白为什么会这样:

Exception Type:     TypeError
Exception Value:    expected str, bytes or os.PathLike object, not tuple

整件东西都没货了。你知道吗

你知道吗型号.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)])

你知道吗视图.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']

新闻/模板/新闻/帖子_表格.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'

Tags: py图像imageformtrueurlosmodels