1

jquery フォーム プラグインhttp://jquery.malsup.com/form/#getting-startedを使用して、画像を django サーバー側にアップロードしています。エラーはありませんが、イメージは MEDIA_ROOT パスに保存されていません。過去 2 日間、さまざまな手法 (プラグインを試した) を試しましたが、何も機能していません。以下に、簡単にするためにすべてを示しました。

私のHTMLコード:

<form id="uploadform" method="post" enctype="multipart/form-data" action="background/">
<input type="file" name="photoimg" id="photoimg" />
</form>

Javascript コード:

https://gist.github.com/2377516

Django Models.py:

class BackgroundModel(models.Model):
    user = models.OneToOneField(User)
    background = models.ImageField(upload_to='backgrounds')

ジャンゴのviews.py:

    def backgroundview(request):
        if request.is_ajax():
            b = request.POST.get('photoimg')        
        try:
            g = BackgroundModel.objects.get(user=request.user)
        except BackgroundModel.DoesNotExist:
            bm = BackgroundModel(background=b)
            bm.user = request.user
            bm.save()
        else:
            g.background = b
            g.save()
        return HttpResponse("")

ジャンゴの設定.py:

MEDIA_ROOT = '/home/nirmal/try/files/'
MEDIA_URL = 'http://localhost:8000/files/'

ジャンゴのurls.py:

url(r'^cover/$', login_required(direct_to_template), {'template': 'cover.html'}),
url(r'^cover/background/$', 'cover.views.backgroundview'), 

誰でも私を助けることができますか?

ありがとう!

4

1 に答える 1

2

Files are in request.FILES not request.POST. You aren't seeing any errors for this because your are never confirming that b actually got a value, though I'm surprised you're not seeing an error for background cannot be null.

Additionally you should try using a ModelForm for this. It will wrap up a lot of the behavior you're having to code in.

于 2012-04-13T16:39:21.403 に答える