我如何在 post 上使用 CreateView 上传图片?

How i can upload an image with CreateView on my post?

我遇到了一些问题,我可以上传字段 'text' 和放置 URLField 的字段 'video' 之类的文本,问题是我可以从管理面板上传图像没有任何问题。但是在从视图中通过 CreateView 进行操作时,我是不可能的。

我被告知将标签 (enctype = "multipart / form-data") 添加到表单并且它有效,但它没有将它上传到 /media/posts/image.jpg,而是尝试将它上传到 (/ media / image .jpg) 最后就是没有上传图片。

我真的只想将图片上传到我的帖子中,如您所见https://plxapp.herokuapp.com/,然后使用头像和 UserProfile 的 header 进行上传。

如果他们有任何应该完成的程序或验证,他们可以在这里告诉我。

我留下我的密码:

模板:

        <form action="" enctype="multipart/form-data" method="post">
            {% csrf_token %}
            <div class="form-group">
                <label for="{{ form.subject.id_text }}">Text</label>
                {{ form.text }}
            </div>
            <div class="form-group">
                <label for="{{ form.subject.id_image }}">Image</label>
                {{ form.image }}
            </div>
            <div class="form-group">
                <label for="{{ form.subject.video }}">Video</label>
                {{ form.video }}
            </div>
            <button type="submit" class="btn btn-success">Publish <span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
        </form>

views.py:

class PostCreateView(generic.CreateView):
    form_class = PostForm
    success_url = reverse_lazy('timeline')
    template_name = 'posts/post_new.html'

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.user = self.request.user
        obj.date_created = timezone.now()
        obj.save()
        return redirect('timeline')

forms.py:

class PostForm(forms.ModelForm):
    text = forms.CharField(
        widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'What are you thinking?', 'maxlength': '200', 'rows': '3'})
)
    image = forms.CharField(
        widget=forms.FileInput(attrs={'class': 'form-control'}), required=False
)
    video = forms.CharField(
        widget=forms.URLInput(attrs={'class': 'form-control', 'placeholder': 'Youtube, Twitch.tv, Vimeo urls.', 'aria-describedby': 'srnm'}), required=False
)

    class Meta:
        model = Post
        fields = ('text', 'image', 'video')

models.py

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.CharField(max_length=200)
    image = models.ImageField(upload_to='posts', blank=True)
    video = models.URLField(blank=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["-date_created"]

    def __str__(self):
        return "{} {} (@{}) : {}".format(self.user.first_name,self.user.last_name, self.user.username,self.text)

Github(来源): https://github.com/cotizcesar/plaxedpy

要将文件字段添加到您的表单,您可以使用 forms 模块中的 FileField,例如 image = forms.FileField()

如果您想修改表单内表单字段的小部件,只需将 widgets 属性 添加到 Meta class。像这样:

class PostForm(Form):
    image = FileField()
    class Meta:
        fields = ('title', 'text')
        widgets = {
            'title': forms.TextInput(attrs={'what': 'ever'}),
             }