import re
from bs4 import BeautifulSoup
from django import template


register = template.Library()

@register.filter(is_safe=True)
def baseurl(html, base):
    if not base.endswith('/'):
        base += '/'

    absurl = re.compile(r'\s*[a-zA-Z][a-zA-Z0-9\+\.\-]*:')  # Starts with scheme:.
    def isabs(url):
        return url.startswith('/') or absurl.match(url)

    soup = BeautifulSoup(html)

    for link in soup.findAll('a', href=True):
        if not isabs(link['href']):
            link['href'] = base + link['href']

    for img in soup.findAll('img', src=True):
        if not isabs(img['src']):
            img['src'] = base + img['src']

    elements = soup.findAll(style=True) # All styled elements.
    for e in elements:
        def func(m):
            url = m.group(2)
            if not isabs(url):
                url = base + url
            return m.group(1) + url + m.group(3)

        e['style'] = re.sub(r'''(url\(\s*)([^\s\)\"\']*)(\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*")([^\s\"]*)("\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*')([^\s\']*)('\s*\))''', func, e['style'])

    return str(soup)

import re
from bs4 import BeautifulSoup
from django import template


register = template.Library()

@register.filter(is_safe=True)
def baseurl(html, base):
    if not base.endswith('/'):
        base += '/'

    absurl = re.compile(r'\s*[a-zA-Z][a-zA-Z0-9\+\.\-]*:')  # Starts with scheme:.
    def isabs(url):
        return url.startswith('/') or absurl.match(url)

    soup = BeautifulSoup(html, 'html.parser')

    for link in soup.findAll('a', href=True):
        if not isabs(link['href']):
            link['href'] = base + link['href']

    for img in soup.findAll('img', src=True):
        if not isabs(img['src']):
            img['src'] = base + img['src']

    elements = soup.findAll(style=True) # All styled elements.
    for e in elements:
        def func(m):
            url = m.group(2)
            if not isabs(url):
                url = base + url
            return m.group(1) + url + m.group(3)

        e['style'] = re.sub(r'''(url\(\s*)([^\s\)\"\']*)(\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*")([^\s\"]*)("\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*')([^\s\']*)('\s*\))''', func, e['style'])

    return str(soup)

Change bs4 html parser to html.parser
Change bs4 html parser to html.parser

This is to fix wrapping with <html> tags.

  import re
  from bs4 import BeautifulSoup
  from django import template
  
  
  register = template.Library()
  
  @register.filter(is_safe=True)
  def baseurl(html, base):
      if not base.endswith('/'):
          base += '/'
  
      absurl = re.compile(r'\s*[a-zA-Z][a-zA-Z0-9\+\.\-]*:')  # Starts with scheme:.
      def isabs(url):
          return url.startswith('/') or absurl.match(url)
  
-     soup = BeautifulSoup(html)
+     soup = BeautifulSoup(html, 'html.parser')
  
      for link in soup.findAll('a', href=True):
          if not isabs(link['href']):
              link['href'] = base + link['href']
  
      for img in soup.findAll('img', src=True):
          if not isabs(img['src']):
              img['src'] = base + img['src']
  
      elements = soup.findAll(style=True) # All styled elements.
      for e in elements:
          def func(m):
              url = m.group(2)
              if not isabs(url):
                  url = base + url
              return m.group(1) + url + m.group(3)
  
          e['style'] = re.sub(r'''(url\(\s*)([^\s\)\"\']*)(\s*\))''', func, e['style'])
          e['style'] = re.sub(r'''(url\(\s*")([^\s\"]*)("\s*\))''', func, e['style'])
          e['style'] = re.sub(r'''(url\(\s*')([^\s\']*)('\s*\))''', func, e['style'])
  
      return str(soup)
  
Change bs4 html parser to html.parser
## Code Before:
import re
from bs4 import BeautifulSoup
from django import template


register = template.Library()

@register.filter(is_safe=True)
def baseurl(html, base):
    if not base.endswith('/'):
        base += '/'

    absurl = re.compile(r'\s*[a-zA-Z][a-zA-Z0-9\+\.\-]*:')  # Starts with scheme:.
    def isabs(url):
        return url.startswith('/') or absurl.match(url)

    soup = BeautifulSoup(html)

    for link in soup.findAll('a', href=True):
        if not isabs(link['href']):
            link['href'] = base + link['href']

    for img in soup.findAll('img', src=True):
        if not isabs(img['src']):
            img['src'] = base + img['src']

    elements = soup.findAll(style=True) # All styled elements.
    for e in elements:
        def func(m):
            url = m.group(2)
            if not isabs(url):
                url = base + url
            return m.group(1) + url + m.group(3)

        e['style'] = re.sub(r'''(url\(\s*)([^\s\)\"\']*)(\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*")([^\s\"]*)("\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*')([^\s\']*)('\s*\))''', func, e['style'])

    return str(soup)

## Instruction:
Change bs4 html parser to html.parser
## Code After:
import re
from bs4 import BeautifulSoup
from django import template


register = template.Library()

@register.filter(is_safe=True)
def baseurl(html, base):
    if not base.endswith('/'):
        base += '/'

    absurl = re.compile(r'\s*[a-zA-Z][a-zA-Z0-9\+\.\-]*:')  # Starts with scheme:.
    def isabs(url):
        return url.startswith('/') or absurl.match(url)

    soup = BeautifulSoup(html, 'html.parser')

    for link in soup.findAll('a', href=True):
        if not isabs(link['href']):
            link['href'] = base + link['href']

    for img in soup.findAll('img', src=True):
        if not isabs(img['src']):
            img['src'] = base + img['src']

    elements = soup.findAll(style=True) # All styled elements.
    for e in elements:
        def func(m):
            url = m.group(2)
            if not isabs(url):
                url = base + url
            return m.group(1) + url + m.group(3)

        e['style'] = re.sub(r'''(url\(\s*)([^\s\)\"\']*)(\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*")([^\s\"]*)("\s*\))''', func, e['style'])
        e['style'] = re.sub(r'''(url\(\s*')([^\s\']*)('\s*\))''', func, e['style'])

    return str(soup)
