Python 使用Ajax时无法将文件上载到Django中的媒体路径

Python 使用Ajax时无法将文件上载到Django中的媒体路径,python,ajax,django,django-views,Python,Ajax,Django,Django Views,我正在尝试将图像文件上载到在my setting.py和中指定的媒体url 在数据库表中存储图像的路径。 但是,当使用Ajax上传图像文件时,我无法实现这一点 template.html <div class="profpic" style="background:url(../../../static/app/img/test.png);background-size:cover"> <input type="file" id="picpath" na

我正在尝试将图像文件上载到在my setting.py和中指定的媒体url 在数据库表中存储图像的路径。 但是,当使用Ajax上传图像文件时,我无法实现这一点

template.html

<div class="profpic" style="background:url(../../../static/app/img/test.png);background-size:cover">
            <input type="file" id="picpath" name="picpath" class="uploadpic" value=""/>
        </div>
视图.py

def saveprof(request):
if request.method == "POST":
    picpathV = request.POST['picpath_Aj']
else:
    profemailV = ''
    response_data = 'Nothing to update!'
    return HttpResponse(response_data, content_type="text/plain")
response_data = 'Empty'
try:
    res=td_table.objects.filter(id=request.session.get('proid')).update(picpath=picpathV)
except:
    response_data = 'Something went wrong!'
return HttpResponse(response_data, content_type="text/plain")
class td_Student(models.Model):
firstname = models.CharField(max_length=300,blank=True)
picpath=models.FileField(upload_to=unique_filename)

def unique_filename(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s_%s.%s" %(uuid.uuid4(),time.strftime("%Y%m%d_%H%M%S"), ext)
    return os.path.join('documents/documents/'+time.strftime("%Y%m%d"), filename)
MEDIA_ROOT = 'C:/DJ/'
MEDIA_URL = '/DJ/'
上面的代码工作正常,但我只能保存类似('C:\fakepath\file.jpg')的文件路径。。并且文件未保存到媒体 Settings.py中提供的路径

当我在视图中使用request.FILES时,当使用Django表单时,我可以上载文件。。但在我的例子中,我只需要使用Ajax函数就可以完成这项工作。 视图代码中可能有什么错误

这是我的模型。py

def saveprof(request):
if request.method == "POST":
    picpathV = request.POST['picpath_Aj']
else:
    profemailV = ''
    response_data = 'Nothing to update!'
    return HttpResponse(response_data, content_type="text/plain")
response_data = 'Empty'
try:
    res=td_table.objects.filter(id=request.session.get('proid')).update(picpath=picpathV)
except:
    response_data = 'Something went wrong!'
return HttpResponse(response_data, content_type="text/plain")
class td_Student(models.Model):
firstname = models.CharField(max_length=300,blank=True)
picpath=models.FileField(upload_to=unique_filename)

def unique_filename(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s_%s.%s" %(uuid.uuid4(),time.strftime("%Y%m%d_%H%M%S"), ext)
    return os.path.join('documents/documents/'+time.strftime("%Y%m%d"), filename)
MEDIA_ROOT = 'C:/DJ/'
MEDIA_URL = '/DJ/'
根据上述逻辑,文件名应类似于“documents/documents/20150716/a1f81a80-ce6f-446b-9b49-716b5c67a46e_20150716_222614.jpg”-此值应存储在我的数据库表中

设置.py

def saveprof(request):
if request.method == "POST":
    picpathV = request.POST['picpath_Aj']
else:
    profemailV = ''
    response_data = 'Nothing to update!'
    return HttpResponse(response_data, content_type="text/plain")
response_data = 'Empty'
try:
    res=td_table.objects.filter(id=request.session.get('proid')).update(picpath=picpathV)
except:
    response_data = 'Something went wrong!'
return HttpResponse(response_data, content_type="text/plain")
class td_Student(models.Model):
firstname = models.CharField(max_length=300,blank=True)
picpath=models.FileField(upload_to=unique_filename)

def unique_filename(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s_%s.%s" %(uuid.uuid4(),time.strftime("%Y%m%d_%H%M%S"), ext)
    return os.path.join('documents/documents/'+time.strftime("%Y%m%d"), filename)
MEDIA_ROOT = 'C:/DJ/'
MEDIA_URL = '/DJ/'

问题不在于Django,而在于您的AJAX帖子,您只是传递名称,因此Django接收并保存名称

解决方案:
一旦用户选择了一个文件,就会发出
更改
事件,在这个更改事件中,您必须使用
事件.target.files
将文件实例存储在局部变量中,并将其传递给
picpath\u Aj'

// Add events
$('input[type=file]').on('change', prepareUpload);

// Grab the files and set them to our variable
function prepareUpload(event)
{
  files = event.target.files;
}
详细指南在这里


另外,带有JS和后端代码的Django解决方案是

谢谢kumar。我会试试这个方法。我想,Django本身可能有某种可能性,我错过了。。但这也是非常有用的。Sathish,Django是服务器端,无法使用path访问客户端文件,因此文件流必须通过JS或HTML表单从客户端上传。我同意。但是我需要用Django编写服务器端代码,以便按照废弃.ie.中给出的示例上传文件。。您是否遇到过使用Ajax和Python django上传文件而不使用html表单的工作示例?嗨,Kumar,我有一个问题。。我可以使用表单上传文件并通过Ajax方法发送表单数据吗??这可能吗?如果可能的话,DJango可以将上传的文件存储在服务器中。是的,我给的JS就是这样做的,它从文件inout字段读取并获取文件实例。因此,您可以在通过Ajax发布时使用该实例,但请阅读完整的指南,内容类型等有一些注意事项。