Answer the question
In order to leave comments, you need to log in
How to implement automatic file save path generation in Django?
There is such a user model
class UserProfile(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100, verbose_name="Имя")
last_name = models.CharField(max_length=100, verbose_name="Фамилия")
study_group = models.CharField(max_length=100, verbose_name="Группа")
def __str__(self):
return self.user.username
class Article(models.Model):
file_obj = models.FileField(upload_to='files', verbose_name='Файл')
def Upload_file(request):
if request.method == 'POST':
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/theory/all')
else:
form = ArticleForm()
return render(request, 'file_upload.html', {'form': form})
Answer the question
In order to leave comments, you need to log in
You can pass a function to the upload_to argument. If you have enough time, then you can:
upload = models.FileField(upload_to='uploads/%Y/%m/%d/')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question