文件上传在Django modelForm [英] File Upload in Django modelForm

查看:344
本文介绍了文件上传在Django modelForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在appengine-django中上传documets。
使用纯django代码成功上传文档[使用python manage.py runsever]。
但是当我试图用appengine项目运行django它会给我错误,

I am trying to upload documets in appengine-django. Docs getting uploaded successfully with pure django code [ using python manage.py runsever ]. But when i am trying to run django with appengine project it gives me error ,

[Errno 30] Read-only file system: u'/home/nishant/workspace1/problemdemo/uploaded_files/1372313114_43_nishant26062013.zip'

由于开发人员对App Engine上的文件系统具有只读权限,因此导致此错误。

是否有其他方式将文档上传到Google云sql?

Is there is another way to upload docs to google cloud sql ?

这是我的代码,

models.py

from django.db import models
import time

# Create your models here.

def get_upload_file_name(instance,filename):
    return "uploaded_files/%s_%s" % (str(time.time()).replace('.','_'),filename)


class Candidate(models.Model):
    title=models.CharField(max_length=20)
    resume=models.FileField(upload_to=get_upload_file_name)

    def __unicode__(self):
        return self.title

forms.py

from django import forms
from actualproblem.models import Candidate

class TestForm(forms.ModelForm):

    class Meta:

        model=Candidate

        fields=('title','resume',)

views.py

# Create your views here.
from django.shortcuts import render
from actualproblem.forms import TestForm

def sampletest(request):


    if request.method=='POST':        
        form = TestForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()

    else:
        form=TestForm()


    return render(request,'profile.html',{'form':form})

如何将documetns上传到Google云sql?

推荐答案

您可以使用 Uploadcare 解决难题,可以帮助您处理情况当你对主机文件系统(GAE,英雄,共享主机或者什么)几乎没有控制权时。上传的文件将存储在Uploadcare中,您将在文件夹中存储文件UUID或URL。

You may solve the conundrum by using Uploadcare, it can help in situations when you have little or no control over host filesystem (GAE, heroku, shared hosting or whatnot). Uploaded files will be stored in Uploadcare and you will store file UUIDs or URLs in your DB.

有Django包: pyuploadcare

披露:我是Uploadcare开发人员之一,发布这个不要无耻地推广服务,而是因为它是为了解决这样的情况而建立的。

disclosure: I am one of Uploadcare developers and am posting this not to shamelessly promote the service but because it was built to solve cases like this one.

这篇关于文件上传在Django modelForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆