My StatcPages model has this type of url:

url(r'^(?P<slug>[a-z0-9_-]+)/$', StaticPageDetailView.as_view(), name='static_page'), 

The slug field is unique = True, but there are other static URLs above, and the slug field can match them. Is it possible to somehow fasten the validator to uniqueness, but supplemented with custom values?

  • Not quite clear what's the field from the database (?). slug is the name of the group, or in this context the name of the parameter that will be passed to the function. The only requirement in regulars is that the group name must be valid and not repeated. - m9_psy
  • In the example I will show, at the moment the structure is as follows: url (r '^ news /', include ('news.urls')), url (r '^ foto-zoopark /', include ('gallery.urls')), url (r '^ (? P <slug> [a-z0-9 _-] +) / $', StaticPageDetailView.as_view (), name = 'static_page'), Slug can be changed through the admin area and it can be matched with news for example and the validator will not give an error, because the field will be truly unique. - pyinto
  • Um, I haven't used jungle for a long time, but are we talking about urls exactly? If you are afraid of cases when the value of the slug parameter is equal to news or foto-zoopark, then you can disable them in the regular schedule - example - m9_psy
  • @ m9_psy, damn it, I'm a fool)) Exactly, thank you very much, I haven’t been sleeping for 3 days, I started doing some kind of nonsense, it's time to sleep)) - pyinto

1 answer 1

If I understand you correctly, here is a guide that will help you get a lot of words that can not be used as new slug:

  1. Install django-extensions

     pip install django-extensions 
  2. Use code:

     # encoding: utf-8 from django.apps import apps from django.conf import settings from django_extensions.management.commands.show_urls import \ extract_views_from_urlpatterns, simplify_regex def get_words(): urlconf = __import__(settings.ROOT_URLCONF, {}, {}, ['']) res = set() view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns) for (func, regex, url_name) in view_functions: res.add(simplify_regex(regex).strip('/').split('/', 1)[0]) return res stop_words = get_words() 
  3. Use stop_words for validation.

PS See show_urls for details.