Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Django1.0でのアップロード画像の保存
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
hirano.19841011@gmail.com  
View profile   Translate to Translated (View Original)
 More options Jun 22, 3:15 am
From: "hirano.19841...@gmail.com" <hirano.19841...@gmail.com>
Date: Mon, 22 Jun 2009 00:15:44 -0700 (PDT)
Local: Mon, Jun 22 2009 3:15 am
Subject: Django1.0でのアップロード画像の保存
はじめまして、平野と申します。

Django1.0を使って画像をアップロードする仕掛けを作っています。
そこで、ImageFieldを用いた画像の保存処理を行わせたいのですが、
うまくいかず詰まってしまっています。

自分はこうやったよーと言ったアドバイスを頂ければと思い投稿しました。
以下、状況となります。
http://d.hatena.ne.jp/Lambda_groove/より)

========================================================

Django1.0を使った画像のアップロードで詰まった。

何がダメだったかまとめてとりあえず対策を調査。

どこかが間違っているコード。

views.pyの抜粋

if request.method == 'POST':
    form = ArticleSubmitForm(request.POST, request.FILES)

    if form.is_valid():
        form.full_clean()
        cleaned_data = form.cleaned_data

        pub_date = forms.DateTimeField()
        cleaned_pub_date = pub_date.clean(datetime.datetime.now())

        article = Article()
        article.subject    = cleaned_data["subject"]
        article.body       = cleaned_data["body"]
        article.photo_name = cleaned_data["photo_name"]
        article.image      = cleaned_data["image"]
        article.pub_date   = cleaned_pub_date

        article.save()

views.pyの抜粋の続き

class ArticleSubmitForm(forms.Form):
    subject    = forms.CharField(label="タイトル",
                                 max_length=200,
                                 required=True)

    body       = forms.CharField(label="本文",
                                 max_length=2000,
                                 widget=forms.Textarea,
                                 required=True)

    photo_name = forms.CharField(label="写真の説明",
                                 max_length=200,
                                 required=True)

    image      = forms.ImageField(label="ファイル",
                                  widget=forms.FileInput,
                                  required=True)

models.py

from django.db import models

class Article(models.Model):
    subject    = models.CharField(max_length=200)
    body       = models.TextField(max_length=2000)
    photo_name = models.CharField(max_length=200)
    pub_date   = models.DateTimeField("date published")
    image      = models.ImageField(upload_to="img")

    class Admin:
        list_display = ("subject",
                        "body",
                        "photo_name",
                        "pub_date",
                        "image",
                        )

状態。

登録処理自体は走る。

そして画像がsettings.pyのMEDIA_ROOT/imgに保存されない。

ディスクの中身を片っ端から探したがどこにも画像はいない。

保存できてないのに例外すら出ないという事はさておき、

バグ取りのとっかかりすらない状態。

対策。

    * リクエスト側の<input type="file" />から送信されるファイルデータは、必ず<form
enctype="multipart/form-data">から送らせること。基本だが忘れてた。
    * PILはインストールしてあるか?ないとsyncdbした際エラーが吐かれる。ImageFieldを使いたかったのでインストールし
た。
    * A2Cさんのブログのやり方でもダメだった。
    * Vistaだからか?と思いLinux環境でも試したがダメ。
    * piro_sukeさんのブログのやり方だと、必要なライブラリを配布しているサイトが死んでて試せず。
========================================================

以上、よろしくお願いします。


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
makoto tsuyuki  
View profile   Translate to Translated (View Original)
 More options Jun 22, 11:21 am
From: makoto tsuyuki <mtsuy...@gmail.com>
Date: Mon, 22 Jun 2009 08:21:34 -0700 (PDT)
Local: Mon, Jun 22 2009 11:21 am
Subject: Re: Django1.0でのアップロード画像の保存
露木です。
#ModelFormを使いたくない理由があるのかな?

http://djangoproject.jp/doc/ja/1.0/ref/forms/api.html#binding-uploade...
http://djangoproject.jp/doc/ja/1.0/topics/http/file-uploads.html#topi...

取り急ぎ

On 6月22日, 午後4:15, "hirano.19841...@gmail.com" <hirano.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
岩下洋治  
View profile   Translate to Translated (View Original)
 More options Jul 19, 10:26 pm
From: 岩下洋治 <fruhlingsstim...@gmail.com>
Date: Sun, 19 Jul 2009 19:26:59 -0700 (PDT)
Local: Sun, Jul 19 2009 10:26 pm
Subject: Re: Django1.0でのアップロード画像の保存
岩下と申します。
私も同じ事ではまっていたのですが、解決しました。
平野さんのソースだと、以下のように書けば動くと思います。

>        article.image      = cleaned_data["image"]

uploadobj = cleand_data["image"]
article.image.save( uploadobj.name, uploadobj )

この辺はドキュメントがわかりにくいですよね。

On 6月22日, 午後4:15, "hirano.19841...@gmail.com" <hirano.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google