title: How i can upload an image with CreateView on my post?
body: <p>I have had some problems, I can upload text like the field 'text' and the field 'video' in which I place a URLField, the problem is that from the administration panel I can upload the image without any problem. But at the time of doing it by CreateView from a view I am not possible.</p>
  
  <p>I was told to add the tag (enctype = "multipart / form-data") to the form and it works, but instead of uploading it to /media/posts/image.jpg it attempts to upload it to (/ media / image .jpg) and the end of it all is that it does not upload the image.</p>
  
  <p>I really just want to upload the images to my posts as you can see here <a href="https://plxapp.herokuapp.com/" rel="nofollow noreferrer">https://plxapp.herokuapp.com/</a> and later do so with the avatar and the header of the UserProfile.</p>
  
  <p>If they have any procedure or validation that should be done, they can tell me here.</p>
  
  <p>I leave my code:</p>
  
  <p>Template:</p>
  
  <pre><code>        &lt;form action="" enctype="multipart/form-data" method="post"&gt;
              {% csrf_token %}
              &lt;div class="form-group"&gt;
                  &lt;label for="{{ form.subject.id_text }}"&gt;Text&lt;/label&gt;
                  {{ form.text }}
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="{{ form.subject.id_image }}"&gt;Image&lt;/label&gt;
                  {{ form.image }}
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="{{ form.subject.video }}"&gt;Video&lt;/label&gt;
                  {{ form.video }}
              &lt;/div&gt;
              &lt;button type="submit" class="btn btn-success"&gt;Publish &lt;span class="glyphicon glyphicon-edit" aria-hidden="true"&gt;&lt;/span&gt;&lt;/button&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>forms.py:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Github (source):
  <a href="https://github.com/cotizcesar/plaxedpy" rel="nofollow noreferrer">https://github.com/cotizcesar/plaxedpy</a></p>