Answer the question
In order to leave comments, you need to log in
How to solve Django file upload error?
Error: ValueError at /music/upload/
The Files could not be created because the data didn't validate.
I don't understand why
Model:
class Files(models.Model):
user = models.ForeignKey(User)
text = models.CharField(max_length=100)
artist = models.CharField(max_length=50)
our_file = models.FileField(upload_to='profile_image')
image = models.ImageField(upload_to='profile_image', blank=True, default='new.jpg')
data_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
class UploadForm(forms.ModelForm):
text = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Введите название песни...'
}
))
artist = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Введите имя исполнителя...'
}
))
class Meta:
model = Files
fields = (
'text',
'artist',
'our_file',
'image',
)
<form action="" method="post">
{% csrf_token %}
{{form.text}}
<br>
{{form.artist}}
<hr>
{{form.our_file}}
<br>
{{form.image}}
<button type="submit" class="btn btn-success btn-lg">Подтвердить</button>
</form>
class Upload(TemplateView):
template_name = 'home/upload.html'
def get(self, request):
form = UploadForm()
args = {
'form': form
}
return render(request, self.template_name, args)
def post(self, request):
form = UploadForm(request.POST)
if form.is_valid():
new = form.save(commit=False)
new.user = request.user
new.save()
return redirect('music:home')
return redirect('music:home')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question