Django integrates the ueditor Rich Text Editor and solves the problem of uploading and displaying images and videos.

Source: Internet
Author: User

Django integrates the ueditor Rich Text Editor and solves the problem of uploading and displaying images and videos.
Generally, the text editor in the django background is relatively simple, so the ueditor editor is added.
Ueditor is an open-source product developed by Baidu. The django version is not available on the official website.
Django-version https://github.com/zhangfisher/DjangoUeditor/archive/master.zip
Decompress the package and copy the DjangoUeditor folder to the django project directory, which is the same as the app directory.

Start modification:
Url. py file

Import fromDjangoUeditorimporturlsasDjangoUeditor_urls
Add route url (R' ^ ueditor/', include (DjangoUeditor_urls ))

Models File
Import fromDjangoUeditor. models import UEditorField
Definition of child segments that require the editor

Context = UEditorField ('content', height = 300, width = 1000, default = '', blank = True, imagePath =" news ", toolbars = 'full ', filePath = 'files ')

The text box size and field default values are defined. imagePath and filePath define the image, video, and file upload paths (relative to settings. MEDIA_ROOT)
Toolbars defines the display complexity of functions, such as mini, besttome, nomal, and full. It is set in DjangoUeditor. settings.

In this way, you can use

However, images and videos cannot be uploaded. the upload fails because the path is open during image uploading. You can upload the video.
You need to modify the file DjangoUeditor. views. The modification function of the save_upload_file file is about 38 lines.

Directly modify the file to perform different operations based on the upload action.

 
 
  1. Def save_upload_file (PostFile, FilePath, action ):

  2. If action = 'uploadfile ':
  3. FilePathWrite = FilePath + '/' + str (PostFile)
  4. Elif action = 'uploadimage ':
  5. FilePathWrite = FilePath + '/' + str (PostFile)
  6. # Elif action = 'uploadvideo ':
  7. # FilePathWrite = videofile (FilePath, '/video /')
  8. Else:
  9. FilePathWrite = FilePath

  10. Try:
  11. F = open (FilePathWrite, 'wb ')
  12. For chunk in PostFile. chunks ():
  13. F. write (chunk)
  14. Failed t Exception as E:
  15. F. close ()
  16. Return u "file writing error:" + E. message
  17. F. close ()
  18. Return u "SUCCESS"
After the "OK" function is changed, you can upload images in the corresponding folder, but the images cannot be displayed in the dialog box. The corresponding path is incorrect. In this case, you need to modify another function.

Modify the DjangoUeditor. views file, which is about 258 rows. You need to splice the returned image path and modify the return_info dictionary again. Then, you can use the upload operation to redefine the returned url.


 
 
  1. Import urllib
  2. If action = 'uploadvideo ':
  3. Url = urllib. basejoin (USettings. gSettings. MEDIA_URL, OutputPathFormat)
  4. Else:
  5. Url = urllib. basejoin (USettings. gSettings. MEDIA_URL, OutputPathFormat) + '/' + upload_file_name

  6. Return_info = {
  7. 'Url': url, # Name of the saved file
  8. 'Original': upload_file_name, # original file name
  9. 'Type': upload_original_ext,
  10. 'State': state, # upload status. If the upload is successful, SUCCESS is returned. Other values are returned to the Image Upload box as they are.
  11. 'SIZE': upload_file_size
  12. }

Now you can upload images, videos, and files. However, there are still some things, such as graffiti, which cannot be used. We haven't studied them yet.


OK can be used.




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.