我尝试从模型表单创建新对象,但不保存对象。
models.py
class VideoFile(models.Model):
name = models.CharField(max_length=200, blank=True)
file = models.FileField(upload_to="video/", validators=[validate_file_extension])
forms.py
class VideoCreateForm(ModelForm):
class Meta:
model = VideoFile
fields = ['name', 'file']
views.py
def add_video(request):
if request.method == "POST":
form = VideoCreateForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save()
messages.success(request, "saved")
else:
messages.error(request, 'No success!.')
return redirect('vtv:video')
else:
form = VideoCreateForm()
return render(request, "vtv/video_add.html", {'form': form})
templates.html
<h3>Add new video</h3>
<form method = "post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
怎么了?