Answer the question
In order to leave comments, you need to log in
How to upload a file in Django by linking it to a specific db entry?
Good afternoon!
There is a blog model with fields id, text, ... , docfile
docfile = models.FileField(upload_to='documents', blank=True)
class FileUploadForm (forms.ModelForm):
class Meta:
model = blog
fields = ('docfile',)
docfile = forms.FileField()
def blog_upload (request, pk):
if request.method == 'POST':
form = FileUploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/success/url/')
else:
form = FileUploadForm()
return render(request, 'upload_doc.html', {'form': form})
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