Pillow Installation
Pillow is a Python image library
>pip Install Pillow
Knowledge Introduction slug
Slug: Used to generate a meaningful (valid, meaninful) URL
Reference (Http://stackoverflow.com/questions/427102/what-is-a-slug-in-django)
For example: Http://stackoverflow.com/questions/427102/what-is-a-slug-in-django behind the "What-is-a-slug-in-django" is the product after the Slug
How to use:
You need to use the Slugify feature:
From Django.utils.text import slugifyslugify (value)
If value is ' Joel is a slug ', the output would be ' joel-is-a-slug '.
It's A-generating a valid URL, generally using data already obtained. For instance, using the title of a article to generate a URL. I ' d advise to generate the slug, using a-function, given a title (or other piece of data), rather than setting it manually .
Slugfield
Also play a similar role, but this is generally used in the background directly add, such as:
This will have a slug box in the background, after filling in, the URL contains the contents of the slug.
Creating the Productimage Class
Class Productimage (models. Model):
Product = models. ForeignKey (Product)
Image = Models. ImageField (upload_to=image_upload_to)
def __unicode__ (self):
Return Self.product.title
Defines the UPLOAD_TO function to handle the save of an uploaded picture, which requires receiving two parameters instance and filename
upload_to may also is a callable, such as a function. This'll be called to obtain the upload path, including the filename. This callable must accept, arguments and return a unix-style path (with forward slashes) to being passed along to the Stor Age system. The arguments are:
Argument |
Description |
Instance |
An instance of the model where the Filefield is defined. More specifically, the "the" the particular instance where the current file is being attached. In very cases, this object is not having been saved to the database yet, so if it uses the default Autofield, it might not yet has a value for its primary key field. |
FileName |
The filename is originally given to the file. This could or May is taken into account when determining the final destination path. |
The entry instance and filename were
Product (IPhone Cover) iphone_cover.jpg
Where instance is the product object that is currently associated with the uploaded image, and filename is the file name
The UPLOAD_TO function returns the file system storage path, the general path
Media_root = Os.path.join (Os.path.dirname (Base_dir), "static_in_env", "Media_root")
Product sub-catalogs can be customized
From Django.utils.text import Slugifydef image_upload_to (instance, filename): title = Instance.product.titleslug = Slugify (title) basename, file_extension = Filename.split (".") New_filename = "%s-%s.%s"% (slug, instance.id, file_extension) return "products/%s/%s"% (slug, new_filename)
Example: Will iphone_cover. jpg (Product Image) on theiPhone Cover (product)
Then the values of title, Slug, basename, file_extension, new_filename are as follows:
IPhone Cover Iphone-cover Iphone_cover jpg iphone-cover-2.jpg
Note the results of duplicate uploads of the same file, taking MP3 player as an example, mp3_player.jpg to product MP3 player
If you are creating productimage,instance for the first time. ID is None
MP3 Player mp3-player mp3_player jpg mp3-player-none.jpg
MP3 Player mp3_player.jpg
The same name, if you make a second change
MP3 Player mp3-player mp3_player jpg mp3-player-3.jpg
MP3 Player mp3_player.jpg
currently: products/mp3-player/mp3-player-3.jpg
The same name, if you continue overwriting, the file will not be overwritten, but instead increase the random number to re-copy a
MP3 Player mp3-player mp3_player jpg mp3-player-3.jpg
MP3 Player mp3_player.jpg
currently: products/mp3-player/mp3-player-3_7kvee47.jpg
Executive Migrate
>python manage.py Makemigrations>python manage.py Migrate
Create admin Interface
From. Models Import Product,variation,productimageadmin.site.register (productimage)
Add picture display in Product_detail_view
The following two displays are shown below
{{Img.image.file}}
{{Img.image.url}}
D:\virtualenv\ecommerce-ws\src\static_in_env\media_root\products\mp3-player\mp3-player-none.jpg
/media/products/mp3-player/mp3-player-none.jpg
Media_url = '/media/' Media_root = Os.path.join (Os.path.dirname (Base_dir), "static_in_env", "Media_root ")
[Ecommerce2] 017 image uploads image upload