Thursday, September 26, 2019

Django translate_url not working with optional parameters

I am using django 1.11.

My url is:
url(r'^apply(?:/(?P<pk>[\d-]+)/(?P<slug>[\w-]+))?.html$', applyview, name='applyview'),


Line to tranlate url:

translate_url('/nl/job/apply.html', "fr")


Django internal function:

def translate_url(url, lang_code):
   
"""
    Given a URL (absolute or relative), try to get its translated version in
    the `lang_code` language (either by i18n_patterns or by translated regex).
    Return the original URL if no translated version is found.
    """

    parsed
= urlsplit(url)
   
try:
        match
= resolve(parsed.path)
   
except Resolver404:
       
pass
   
else:
        to_be_reversed
= "%s:%s" % (match.namespace, match.url_name) if match.namespace else match.url_name
       
with override(lang_code):
           
try:
                url
= reverse(to_be_reversed, args=match.args, kwargs=match.kwargs)
           
except NoReverseMatch:
               
pass
           
else:
                url
= urlunsplit((parsed.scheme, parsed.netloc, url, parsed.query, parsed.fragment))
   
return url


Django can not tranlate url  get an error NoReverseMatch because kwargs are None. ({'pk': None, 'slug': None}).
By my parameters are optional. 


Does anyone have an idea, how can that be solved?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/92288a98-3083-4d3c-a52d-78aa90696557%40googlegroups.com.

No comments:

Post a Comment