Skip to content

Commit

Permalink
feat: example for FormView, #10
Browse files Browse the repository at this point in the history
  • Loading branch information
escaped committed Oct 7, 2018
1 parent 5595954 commit 8c3135b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
19 changes: 19 additions & 0 deletions test_proj/media_library/templates/video_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
</head>
<body>
<h1>Video Upload</h1>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>

<h2>Uploaded Videos</h2>
{% for video in videos %}
{{ video.file.name }}
(Duration: {{ video.duration }}s, {{ video.width }}x{{ video.height }})
<br>
{% endfor %}
</body>
</html>
29 changes: 29 additions & 0 deletions test_proj/media_library/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django import forms
from django.views.generic import FormView

from video_encoding.fields import VideoField

from .models import Video


class VideoForm(forms.ModelForm):

class Meta:
fields = ('file',)
model = Video


class VideoFormView(FormView):
form_class = VideoForm

success_url = '/'
template_name = 'video_form.html'

def get_context_data(self, *args, **kwargs):
context = super(VideoFormView, self).get_context_data(*args, **kwargs)
context['videos'] = Video.objects.all()
return context

def form_valid(self, form):
form.save() # store video to database
return super(VideoFormView, self).form_valid(form)
8 changes: 6 additions & 2 deletions test_proj/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.conf.urls import include, url
from django.conf.urls import url
from django.contrib import admin

from .media_library.views import VideoFormView


urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
url(r'^', VideoFormView.as_view()),
]

0 comments on commit 8c3135b

Please sign in to comment.