Django Image Upload,
To perform image operations in python, you need to install the installation package PIL.
pip install Pillow==3.4.1
There are two ways to upload images in Django:
- Upload images in admin
- Upload images in a custom form
- After uploading an image, store the image on the server, and store the image path in the table.
Define the static file search path in the test5/settings. py file
STATIC_URL = '/static/'STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'),]MEDIA_ROOT=os.path.join(BASE_DIR,"static/media")
Create a model class containing images
- Define the attributes of the model class as the models. ImageField type.
- Open the booktest/models. py file and define the model class PicTest.
class PicTest(models.Model): pic = models.ImageField(upload_to='booktest/')
Return to the command line to generate the migration
python manage.py makemigrations
Here, I used previously written projects and previously generated migration. This time, I added a new table to it, opened the booktest/migrations/0001_initial.py file, and deleted the AreaInfo section, because this table already exists
Return to the command line and execute the migration.
python manage.py migrate
Because no books or hero models are defined, the system prompts "Delete?". Enter "no" and press enter, indicating that the models will not be deleted.
If the migration fails, change booktest/migrations/0001_initial.py to the name booktest/migrations/0003_initial.py because the database has this name.
Return to the command line and execute the migration.
python manage.py migrate
Create the media directory under the static directory, and then create the application name directory. In this example, booktest is used to save the uploaded image.
Upload images in admin
Open the booktest/admin. py file and register PicTest
from django.contrib import adminfrom models import *admin.site.register(PicTest)
Run the server, enter the following URL
http://127.0.0.1:8000/admin/
Click "Add" to Add data to open the new page.
- Select an image and click "save" to upload the image.
- Return to the database command line to query the data in the pictest table, as shown in figure
The image is saved to the static/media/booktest/directory, as shown in figure
Note: The setting configuration must be correct,
STATICFILES_DIRS=[os.path.join(BASE_DIR,'static'),]MEDIA_ROOT=os.path.join(BASE_DIR,'static/media')
Upload images in a custom form
Open the booktest/views. py file and create the view pic_upload.
def pic_upload(request): return render(request,'booktest/pic_upload.html')
Open the booktest/urls. py file and configure the url
url(r'^pic_upload/$', views.pic_upload),
Create a template pic_upload.html In the templates/booktest/directory.
Define the upload form in the template. The requirements are as follows:
- Form attributes enctype = "multipart/form-data"
- Form method is post
- The input type is file.
<Html>
Open the booktest/views. py file and create the view pic_handle. This file is used to receive the form and save the image.
The FILES attribute of the request object is used to receive the requested file, including the image
From django. core. files. storage import FileSystemStorage # mainly used for file IO operations def pic_handle (request): f1 = request. FILES. get ('pic ') f2 = FileSystemStorage () # f2.save ('filename', file content) r = f2.save ('booktest/% s' % (f1.name), f1) # return the file name. If the file name exists, a non-repeated name p1 = PicTest () p1.pic = r p1.save () return HttpResponse ('OK') will be created ')View Code
Open the booktest/urls. py file and configure the url
url(r'^pic_handle/$', views.pic_handle),
Run the server and enter the following URL in the browser:
http://127.0.0.1:8000/pic_upload/
Select a file and Click Upload Image
The Image Upload directory is shown in figure
This is just the code for uploading images. To save data, you need to create a PicTest object to save it.
Show Image
Open the booktest/views. py file and create the view pic_show
from models import PicTest...def pic_show(request): pic=PicTest.objects.get(pk=1) context={'pic':pic} return render(request,'booktest/pic_show.html',context)View Code
Open the booktest/urls. py file and configure the url
url(r'^pic_show/$', views.pic_show),
Create a template pic_show.html In the templates/booktest/directory.
<Html>
Run the server and enter the following URL in the browser:
http://127.0.0.1:8000/pic_show/