Advertisement
kastielspb

dynamic seo url pattern

Jul 8th, 2020 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. import re
  2. from typing import Optional
  3.  
  4. from seo.models.url_based import UrlSeo
  5. from seo.utils import get_path_from_request
  6.  
  7.  
  8. def get_url_seo(request: 'HttpRequest') -> Optional['UrlSeo']:
  9.     """
  10.    Return `UrlSeo` instance filtered by the current path with or without
  11.    an appended query string, if applicable by settings
  12.    """
  13.     path = str(get_path_from_request(request))
  14.     for obj in UrlSeo.objects.exclude(is_default=True):
  15.         pattern = str(obj.url).replace('*', '(.*)')
  16.         if re.search(pattern, path):
  17.             return obj
  18.     return UrlSeo.objects.filter(is_default=True).first()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement