Django Admin Implementation Upload picture verification function Introduction

Source: Internet
Author: User
Tags django website
Django offers many features for future developers: a proven standard library, an active user community, and all the benefits of the Python language. While other WEB frameworks claim to provide the same content, Django is unique in that it has built-in management application--admin.

Admin provides an out-of-the-box advanced Create-read-update-delete (CRUD) feature that reduces the time it takes to repeat work. This is key to many WEB applications, where programmers can quickly explore their database models at development time, and non-technical end users can use admin to add and edit site content at deployment time.

There is a ImageField field in my models, which is used to save the user's avatar, I want to check the size of the avatar when I upload it via Django Admin, if it is too big, it will be error and not saved.

There are many methods on the Internet, some through the third-party software implementation, and some through their own form verification, I think too complex, its own requirements are not high, just want the simplest way.

The following method is done by overwriting admin. Modeladmin Save_model () to verify the image size, if the picture is greater than 20K, error and not save:

From Django.contrib import messagesclass yourmodeadmin (admin. Modeladmin): ... def save_model (self, request, obj, form, change): #不保存大图片if obj.picture and Obj.picture.size > 20480: Messages.set_level (Request, messages. ERROR) Messages.error (Request, ' The picture\ ' s too large. It\ ' s supposed smaller than 20K. ') Else:obj.save ()

Finally show a well-implemented Django website, thanks to Andrew Liu's online tutorials:

The above content to introduce the Django Admin implementation of the upload image verification function of the relevant knowledge, I hope to help you above!

More Django admin implement upload picture Check function Introduction related articles please follow topic.alibabacloud.com!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.