Django--ImageField upload image change avatar

Source: Internet
Author: User

Django--ImageField upload image change avatar avatar Djangoimagefield

After almost one weeks of free time, Django was lost in the mood to upload a picture.

Defining Model model.py
headImg   = models.ImageField(upload_to = ‘img‘, default="img/4.jpg")

ImageField is a Django built-in data type that can be processed in a more convenient time picture
Upload_to means that the directory to upload will be automatically created in the media directory without a slash, but in practice it doesn't seem to work

The default is to indicate a path to the image, and you can't always start with alt.

Modify Configuration setting.py
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(‘__file__‘)))MEDIA_ROOT = os.path.join(PROJECT_ROOT, ‘media/img‘)MEDIA_URL = "/head/"

Project_root represents the root directory of the project

After stitching the media_root is the absolute path of our avatar, MEDIA created in the root directory, the IMG directory is created by myself, may be the wrong posture, anyway, the above mentioned upload_to did not automatically create a good

Another is media_url, this is our visit to the picture link, for example if our media_root is d:/project_name/media/img/words, when we visit 127.0.0.1:8000/HEAD/IMG/4. JPG, in fact, is to visit d:/project_name/media/img/4.jpg, their own practice to understand or with the online blog some of the discrepancy

So we can represent the picture in HTML.

Learn more about this, you can refer to here, but in my understanding is a discrepancy, maybe I was wrong, it seems to be upload_to here problems

Django under Media_root, Media_url, Static_root, Static_url doubts

It is worth noting that although Media_root is also stored static files, but should be different from static, do not think that the configuration of the static can be dissimilar media, personal understanding, should be the ImageField media related connection bar

Front

To the front end, the creation of forms like this is ignored, you can use Django forms directly, or you can use HTML expressions, but in <form> tags, you should add this property

 enctype="multipart/form-data"

If not added, the uploaded image will become empty, for reasons unknown

Backstage views.py

Naturally, the background is processed, that is, views.py.

The first is to get the picture information of the foreground, and the second input_img to indicate the property name of the upload tag.
The first input_img is an object with properties such as name, URL, etc.

if request.method == ‘POST‘:input_img      = request.FILES[‘input_img‘]

Then the database is updated.

models.UserProfile.objects.filter(username=username).update(headImg = input_img)

The username here is the session value that I saved when I logged in, how to get it or see a person

Ben came to this step is already done, because see database data is updated, and a lot of blog material said ImageField will deal with a lot of trivia, including the picture to put in the folder

However, there is no, the corresponding IMG directory does not appear in the new avatar file, so the display can only be not Found, or, my posture is wrong

View the relevant documents, you can only save the picture directly with the Python code implementation

from PIL import Imageif input_img:img=Image.open(input_img)img.save(‘E:/package_1.0/media/img/‘+input_img.name)

The PIL library is used here to get a basic look at what's available.

There is a problem, that is, the path of save, using the relative path has been wrong, can only be changed to absolute path

And then it's done, but it's still a long way off.

Summarize

It's been a long time, but it's a short story to write about, or a little more.

If in the future, if you encounter the case of the error when knocking on the example, or good to understand the principle of it, so the road may be comforted many, of course, the time allows the case

Django--ImageField upload image change avatar

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.