开发者

image upload in django

Could 开发者_运维技巧someone point me towards an example on image uploading in django. I have tried the documentation and the djangobook, but all I found were examples with file uploads. I am new to Django and I would like to first try to see how an example works before writing my own form. Thanks a lot.


You can check out these links. just see this meets your requirements. Using ajax and django

Django Photologue


Maybe this will help a little:

forms.py

class SomeForm(forms.Form):
    picture = forms.ImageField(required=False)

views.py

if request.method == 'POST':
    form = SomeForm(request.POST, request.FILES)
    if form.is_valid():
        dest_file = open('some_image_file.jpg', 'wb+')
        for chunk in  request.FILES['picture_field'].chunks():
            dest_file.write(chunk)
        dest_file.close()

I haven't ran this peace of code, could be that I miss something... But in general it is like this and the file will be saved to the disk (by default chunks should be 2.5 megs). Just remember that in template where is your form, form must have enctype="multipart/form-data".

When you have file savedm you can use PIL to resize, add watermarks and everything you like to edit image ;)

Good luck. Ignas


It is a bit outdated but still works with django 1.3

http://www.carcosa.net/jason/software/django/stockphoto/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜