1

I have been trying to add a view that uploades videos and displays them in the main template, well while adding code, I realized that the view that uploades the file isn't being rendered while the view that shows the uploded file in the template gets rendered but it doesnt show anything because nothing is being uploded. I dont know where the error might be but I think it is on the views.py, maybe the urls.py.

views.py

    def upload(request):
      if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
          instance = form.save(commit=False)
          instance.user = request.user
          instance.save()
          return redirect('home')
          print('succesfully uploded')
      else:
        form = PostForm()
        print('didnt upload')
      return render(request, 'home.html', {'form': form})

    def home(request):
      contents = Post.objects.all()
      context = {
        "contents": contents,
      }
      print("nice")
      return render(request, 'home.html', context)

urls.py

    urlpatterns = [
      path('', views.home, name='home'),
      path('upload', views.upload, name='upload'),
    ]

models.py

    class Post(models.Model):
      text = models.CharField(max_length=200)
      video = models.FileField(upload_to='clips', null=True, blank="True")
      user = models.ForeignKey(User, related_name='imageuser', on_delete=models.CASCADE, default='username')

      def __str__(self):
        return str(self.text)

forms.py

     class PostForm(forms.ModelForm):
       class Meta:
         model = Post
         fields = ('text', 'video')
         exclude = ['user']

home.html (uplodes the content and displays it)

    <div class="user-content">
      {% for content in contents %}
        <li class="">{{ content.text }}
          {% if content.video %}
            <video class="video" width='400'>
              <source src='{{ content.video.url }}' type='video/mp4'>
            </video>  
          {% endif %}
        </li>     
      {% endfor %}
    </div>
    <div class="uplodes">
      <form method="post" enctype="multipart/form-data">        
        {% csrf_token %}
        <input type="text" name="text" placeholder="Add a comment..." required="" id="id_text">
        <input type="file" name="video" id="id_video">
        <button class="submit-button" type="submit">Save</button>
      </form>
    </div>
Juan Martin Zabala
  • 743
  • 2
  • 9
  • 29
  • possible duplicate of https://stackoverflow.com/questions/56057223/video-upload-and-display-on-a-django-website – ytsejam Jun 02 '20 at 21:52
  • @ytsejam that will not work for me because I already have an accounts view in which the user can upload a profile picture and works perfectly fine so I am wondering If you could help me please becausae I dont know what to do – Juan Martin Zabala Jun 02 '20 at 22:07
  • sorry did you check the answer on the link, I have sent you ? , there is a clear explanation how to do that. do you have MEDIA settings in your settings.py. – ytsejam Jun 02 '20 at 22:11
  • @ytsejam Yes I do have and if I didnt at least the text field would be displaying, do you have any other idea? – Juan Martin Zabala Jun 02 '20 at 22:14
  • so your form is not uploading am I understanding correctly ? or it uploads you cant view video? – ytsejam Jun 02 '20 at 22:18
  • yes the form doesnt upload, I now that because nothing from that view is printing, except from "nice" which is from the other view – Juan Martin Zabala Jun 02 '20 at 22:20
  • 1
    if your upload form is in home view than you have to have an action for your form.
    – ytsejam Jun 02 '20 at 22:24
  • @ytsejam Hey, the code seems to work! thank you, I actually tryed that but without the url tags and didnt work but now it is!! do you have any idea of how I can set a time limit on the video? – Juan Martin Zabala Jun 02 '20 at 22:30
  • no clue, https://stackoverflow.com/questions/58552487/how-to-calculate-video-length-and-thumbnail-of-video-in-django – ytsejam Jun 02 '20 at 22:33

0 Answers0