lqez / django-summernote

Simply integrate Summernote editor with Django project.
MIT License
1.05k stars 227 forks source link

Image source address issue in Rest API #489

Open skilljobs opened 1 year ago

skilljobs commented 1 year ago

Suppose images are uploaded from admin.example.com and it stores like "/media/django-summernote/2023-1-11/adsdfsfdf.png" Not the full URL. So in API, the url is not containing main site. How to fix it?

uktamjon-komilov commented 1 year ago

I am having the same issue.

uktamjon-komilov commented 1 year ago

Modified the content before returning the response in the view using util function:

from bs4 import BeautifulSoup

def normalize_html_image_urls(request, content):
    site_url = request.build_absolute_uri('/')

    # Use BeautifulSoup to parse the HTML
    soup = BeautifulSoup(content, 'html.parser')

    # Find all <img> tags and update their src attribute
    for img in soup.find_all('img'):
        if img.has_attr('src'):
            src = img['src']
            if not src.startswith('http'):
                img['src'] = site_url[:-1] + src

    # Convert the updated HTML back to a string
    updated_content = str(soup)

    return updated_content
mahmudtopu3 commented 1 year ago

Well it takes time to do so but I have changed their source code. https://github.com/summernote/django-summernote/blob/main/django_summernote/views.py You can override class SummernoteUploadAttachment(UserPassesTestMixin, View):

uktamjon-komilov commented 1 year ago

Can you give example usage?