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!