[Python] Image upload in CreateView

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a CreateView in my project to post a tweet-like-post. It has a title and content. It look like this in views.py

    class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content']


    def form_valid(self, form): # Author for post (required- if no- error)
    form.instance.author = self.request.user
    return super().form_valid(form)


    And I would like to add picture-upload column to make a persons able to upload a picture with the post.

    My models.py looks like this

    class Post(models.Model):
    title = models.CharField(max_length=100, verbose_name='タイトル')
    content = models.TextField(verbose_name='内容')
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
    return self.title
    def get_absolute_url(self):
    return reverse('Post-detail', kwargs={'pk': self.pk})


    I already have a uploaded_pic folder in my static with 777 permision. Could you please tell me how to add there working picture-upload column

    Continue reading...

Compartilhe esta Página