我正在制作一种库存,其中现有项目的ID写在文本文件中,我只需要将文本文件上传到服务器。 我已经将视频,图像和TIFF上传到同一台服务器而没有任何问题,但我不知道为什么它现在不能正常工作!
我的模特:
def inventory_file_path(instance, file_name):
name = instance.date.__str__()+'.txt'
path = concatenate_paths([inventory_folder, name])
return path
class Inventory(models.Model):
date = models.DateField('Date', null=True, blank=True)
crosses = models.ManyToManyField(Cross,verbose_name='Crosses scanned', null=True, blank=True)
file = models.FileField(upload_to=inventory_file_path, blank=True, null=True)
表格:
class InventoryForm(forms.ModelForm):
class Meta:
model = Inventory
exclude = [InventoryFields.crosses]
file = forms.FileField()
def __init__(self, *args, **kwargs):
super(InventoryForm, self).__init__(*args, **kwargs)
end_year = datetime.date.today().year + 1
self.fields[InventoryFields.date].widget = forms.SelectDateWidget(years=range(start_year, end_year))
self.file = forms.FileField()
观点:
def new_inventory(request):
if request.method == GET:
inv = Inventory()
inv.date = datetime.date.today()
form = InventoryForm(instance=inv)
if request.method == 'POST':
form = InventoryForm(request.POST, request.FILES)
if form.is_valid():
inv = form.save()
当我检查数据库时,正在保存清单对象并且它的日期是正确的但是文件没有上传。此外,没有调用upload_to方法