Django形式的预填充图像字段不起作用

时间:2018-11-05 12:39:26

标签: python html django forms image

我正在尝试以django形式预填充图像字段,但是,我似乎无法使其正常工作。这是我所做的:

Views.py

class EditUnpublished(TemplateView):
template_name = 'adminpage/editUnpublished.html'

    def get(self, request, id):
        if not is_authenticated(request.user):
            return render(request, template_name_not_authenticated)

        post = Unpublished.objects.get(id=id)
        form = PublishForm(
            {
                'title': post.title,
                'text': post.text,
                'user': post.user,
                'image': post.image.url,
                'users': post.users,
                'tags': post.tags,
                'copyeditor': post.copyeditor,
                'comments': post.comments,
            }
        )
        if not(request.user == post.copyeditor):
             form.fields['comments'].widget.attrs['readonly'] = True

        context = {
            'post': post,
            'form': form
        }
        return render(request, self.template_name, context)

Forms.py

class PublishForm(forms.ModelForm):
    title = forms.CharField(required=False)
    text = forms.TextInput()
    image = forms.ImageField(required=False)
    users = forms.ModelChoiceField(queryset=User.objects.all(), 
                               widget=forms.Select(), required=False)
    tags = forms.ModelChoiceField(queryset=Tags.objects.all(), 
                               widget=forms.Select(), required=False)
    copyeditor = forms.ModelChoiceField(queryset=User.objects.filter(groups__name__in=['Editor']),
                                    required=False, to_field_name="id")
    comments = forms.CharField(widget=forms.Textarea(), required=False)

    class Meta:
        model = PostsTwo
        fields = ('title', 'text', 'image', 'users', 'copyeditor', 
                           'tags', 'comments')

Models.py

class Unpublished(models.Model):
    title = models.CharField(max_length=500)
    user = models.ForeignKey(User, related_name="owner", default=None, 
                     on_delete=models.CASCADE, blank=True, null=True)
    text = models.TextField()
    image = models.FileField(upload_to='img', default='img/None/no- 
                    img.jpg', blank=True, null=True)
    created_at = models.DateTimeField(default=datetime.now, blank=True)
    users = models.ForeignKey(User, related_name='contributors', 
                    on_delete=models.CASCADE, blank=True, null=True)
    tags = models.ForeignKey(Tags, on_delete=models.CASCADE, 
                    blank=True, null=True, unique=False)
    copyeditor = models.ForeignKey(User, unique=False, 
 limit_choices_to={'groups__name': "Editor"}, on_delete=models.CASCADE, 
                    blank=True, null=True)
    comments = models.CharField(max_length=500000, blank=True, 
                    null=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "Unpublished"

我想要的是,当用户单击帖子时,将预先填充数据库中包含所有现有数据的表单。除映像外的所有内容都已预先填充。我能做什么? :)

0 个答案:

没有答案