我试图围绕图像和Form handling with class-based views。所以我正在玩这个简单的模型。使用CreateView时,我是否必须使用form_class使用自定义表单覆盖它,以便成功上传/保存和映像?
我无法弄清楚为什么我会这样做:
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']
新闻/模板/消息/ 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'
答案 0 :(得分:0)
您已将MEDIA_ROOT设置为元组,但它应该是一个字符串。
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')