gpt4 book ai didi

python - Django:上传文件并保存到磁盘

转载 作者:行者123 更新时间:2023-11-28 17:44:13 25 4
gpt4 key购买 nike

我正在尝试通过项目中的表单上传一些文件并将文件保存到磁盘。目前,我能够将数据插入数据库 (postgreSQL),但数据并未写入磁盘。我环顾四周,使用 modelform 并调用 save 应该可以解决所有问题(但显然我错过了什么?)。感谢您的帮助。

这是我的代码:views.py(用于上传与工作相关的文件)

# Upload files to a Job (Developer)
@login_required()
@user_passes_test(lambda u: u.groups.filter(name='Developer').exists(), login_url='/login/', redirect_field_name='not allowed')
@require_http_methods(['POST'])
def job_file_upload(request, jobid):

# Get the Job
job = Jobs.objects.get(id=jobid)
fileform = JobFileSubmitForm(request.POST, request.FILES)
if fileform.is_valid():
jfs = fileform.save(commit=False)
file = request.FILES['file']
jfs.user_id = request.user.id
jfs.job_id = jobid
jfs.file = file.name
jfs.uploadDate = datetime.now()
# Save to DB
jfs.save()

return redirect('view_job', jobid=jobid, slug=job.slug)

models.py(保存文件数据并保存到正确路径的DB)

from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location='/media/')

...


def upload_job_file_path(instance, filename):
return os.path.join('/uploads/job_meta/files/', instance.id, filename)


# Submit Files for a Job (Developer Role)
class JobFileSubmit(models.Model):
job = models.ForeignKey(Jobs)
user = models.ForeignKey(User)
file = models.FileField(storage=fs, upload_to=upload_job_file_path, blank=False, null=False)
uploadDate = models.DateTimeField(auto_now=True)

forms.py

class JobFileSubmitForm(forms.ModelForm):

class Meta:
model = JobFileSubmit
fields = 'file',

def save(self, commit=True):
jobfilesubmit = super(JobFileSubmitForm, self).save(commit=False)

if commit:
jobfilesubmit.save()
return jobfilesubmit

view.html

                            <form method="post" action="/job/job_file_upload/j{{ job.id }}/" class="form-inline btn-group" enctype="multipart/form-data">
{% csrf_token %}
<div class="span6 inline">
<label class="control-label">Attach Files: </label>{{ job_file_submit.file }}
<p class="help-block">Attach files that go with this Job.</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success btn-med pull-left"><i class="fa fa-check-circle"></i> Attach Files</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>

TL;DR:目前表单提交,插入到数据库中,但数据没有写入磁盘,也没有写入它应该写入的动态路径。例如,数据未写入:

/media/uploads/job_meta/files/{{job.id}}/{{file.name}}

感谢您花时间阅读本文!非常感谢任何帮助!

最佳答案

我不确定这是否是最终答案,但在这里编写代码块更容易

upload_job_file_path 需要从 MEDIA_ROOT 返回一个相对路径。如果我理解正确的话,id 是实例的 job_id 而不是它的 id。这样下面的函数应该可以工作。(可能是FileField必须在代码中的job字段下面)

模型.py

def upload_job_file_path(instance, filename):
return 'uploads/job_meta/files/%s/%s' % (instance.job_id, filename)


class JobFileSubmit(models.Model):
job = models.ForeignKey(Jobs)
user = models.ForeignKey(User)
file = models.FileField(upload_to=upload_job_file_path, blank=False, null=False)
uploadDate = models.DateTimeField(auto_now=True)

我不太熟悉模式形式,但我认为保存方法也应该处理文件上传。所以这可能就足够了

View .py

if fileform.is_valid():
jfs = fileform.save(commit=True)

关于python - Django:上传文件并保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com