• <bdo id='J8qPl'></bdo><ul id='J8qPl'></ul>

      <legend id='J8qPl'><style id='J8qPl'><dir id='J8qPl'><q id='J8qPl'></q></dir></style></legend>
    1. <tfoot id='J8qPl'></tfoot>

      <small id='J8qPl'></small><noframes id='J8qPl'>

      <i id='J8qPl'><tr id='J8qPl'><dt id='J8qPl'><q id='J8qPl'><span id='J8qPl'><b id='J8qPl'><form id='J8qPl'><ins id='J8qPl'></ins><ul id='J8qPl'></ul><sub id='J8qPl'></sub></form><legend id='J8qPl'></legend><bdo id='J8qPl'><pre id='J8qPl'><center id='J8qPl'></center></pre></bdo></b><th id='J8qPl'></th></span></q></dt></tr></i><div id='J8qPl'><tfoot id='J8qPl'></tfoot><dl id='J8qPl'><fieldset id='J8qPl'></fieldset></dl></div>

      Django i18n_patterns:resolve() 没有按预期工作

      Django i18n_patterns: resolve() doesnt work as expected(Django i18n_patterns:resolve() 没有按预期工作)

      • <small id='c4YtZ'></small><noframes id='c4YtZ'>

        <tfoot id='c4YtZ'></tfoot>

          <bdo id='c4YtZ'></bdo><ul id='c4YtZ'></ul>
          <i id='c4YtZ'><tr id='c4YtZ'><dt id='c4YtZ'><q id='c4YtZ'><span id='c4YtZ'><b id='c4YtZ'><form id='c4YtZ'><ins id='c4YtZ'></ins><ul id='c4YtZ'></ul><sub id='c4YtZ'></sub></form><legend id='c4YtZ'></legend><bdo id='c4YtZ'><pre id='c4YtZ'><center id='c4YtZ'></center></pre></bdo></b><th id='c4YtZ'></th></span></q></dt></tr></i><div id='c4YtZ'><tfoot id='c4YtZ'></tfoot><dl id='c4YtZ'><fieldset id='c4YtZ'></fieldset></dl></div>
            • <legend id='c4YtZ'><style id='c4YtZ'><dir id='c4YtZ'><q id='c4YtZ'></q></dir></style></legend>
                  <tbody id='c4YtZ'></tbody>
              1. 本文介绍了Django i18n_patterns:resolve() 没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                解决这个问题后这里,还有一个:如果你在这里使用翻译 url 系统 https://docs.djangoproject.com/en/1.8/topics/i18n/translation/ 你会看到你添加了像 urlpatterns += i18n_patterns(...).

                After solving this problem here, there's another one: if you use the translation url system here https://docs.djangoproject.com/en/1.8/topics/i18n/translation/ you will see you add patterns like urlpatterns += i18n_patterns(...).

                问题是基本网址没有没有考虑语言,即:

                The problem is that the base url without the language is not taken in account ie:

                • resolve('/fr/produits/') 有效,
                • resolve('/produits/') 不起作用并引发 404.
                • resolve('/fr/produits/') works,
                • but resolve('/produits/') doesnt work and raises 404.

                如何解决?

                这是我的网址:

                urlpatterns = [
                    url(r'^debug/?$', p_views.debug, name='debug'),
                    url(r'^i18n/', include('django.conf.urls.i18n')),
                    url(r'^login/(w*)', p_views.login, name='login'),
                    url(r'^admin/', include(admin_site.urls)),
                    url(r'^public/(?P<path>.*)$',
                        'django.views.static.serve',
                        {'document_root': settings.MEDIA_ROOT},
                        name='url_public'
                        ),
                ]
                urlpatterns += i18n_patterns(
                    url(_(r'^produits/detail/(?P<slug>[a-zA-Z0-9-_]+)/$'),
                        p_views.ProduitDetailView.as_view(), name='produits_detail'),
                    url(_(r'^produits/'),
                        p_views.IndexView.as_view(), name='produits_index'),
                )
                

                这是我制作的非常简单的 URL-tester(对应于 /debug 视图):

                And here's the very simple URL-tester I've made (which corresponds to the /debug view):

                def debug(req):
                
                    def test(url):
                        try:
                            return u'<pre>{0} {1}</pre>'.format(url, resolve(url))
                        except Resolver404:
                            return u'<pre>{0} {1}</pre>'.format(url, 'None')
                
                    response = HttpResponse()
                    response.write(test('produits'))
                    response.write(test('produits/'))
                    response.write(test('/produits'))
                    response.write(test('/produits/'))
                    response.write(test('/fr/produits'))
                    response.write(test('/fr/produits/'))
                    response.write(test('/en/products/'))
                    response.write(test('/sv/produkter/'))
                    return response
                

                这是 http://localhost:8000/debug 页面:

                produits None
                produits/ None
                /produits None
                /produits/ None
                /fr/produits None
                /fr/produits/ ResolverMatch(func=produits.views.IndexView, args=(), kwargs={}, url_name=produits_index, app_name=None, namespaces=[])
                /en/products/ None
                /sv/produkter/ None
                

                最后三行都应该返回 ResolverMatch(...) 因为它们都是有效的 URL.

                The three lastest lines should all return ResolverMatch(...) because they are all valid URLs.

                推荐答案

                Django 的 url 解析器仅适用于当前语言.因此,在尝试使用特定语言解决 URL 之前,您需要切换语言,使用 translation.activate.

                Django's url resolvers only work on current language. So you will need to switch language before attempting to solve an url in a specific language, using translation.activate.

                为了解析 url,这意味着你必须事先知道语言,切换到它然后才解析(基本上 localemiddleware 会为你做什么).

                For resolving the url, that means you must know the language beforehand, switch to it and only then resolve (basically what the localemiddleware will do for you).

                为了反转 url,这意味着您可能应该使用其名称反转 url.您将以当前语言取回网址.我现在无法测试,但它应该像这样工作:

                For reversing the url, that means you should probably reverse the url using its name. You'll get back the url in current language. I cannot test right now, but it should work like this:

                from django.utils import translation
                translation.activate('fr')
                reverse('produits_index')    # /fr/produits/
                translation.activate('en')
                reverse('produits_index')    # /en/products/
                

                如果您确实设法获得了一个 ResolverMatch 对象,那么您可以将 url 名称作为属性,方便地命名为 url_name.

                If you did manage to get a ResolverMatch object, you have the url name as an attribute on it, conveniently named url_name.

                我希望它有所帮助,我有点不清楚你想要做什么.随时评论/编辑您的问题,我会尝试更新此答案.

                I hope it helps, I am a bit unclear as to what you are trying to do. Feel free to comment/edit your question and I'll try to update this answer.

                Olivier Pons 更新

                Update by Olivier Pons

                这是有效的解决方案:

                这是我的工作解决方案,它接近光谱,但按我想要的方式工作:

                here's my working solution, which is close to spectras, but works the way I wanted:

                # (!) resolve() use current language
                #     -> try to guess language then activate BEFORE resolve()
                lg_curr = translation.get_language()
                lg_url = get_language_from_path(url) or lg_curr
                translation.activate(lg_url)
                try:
                    resolve(url)
                    req.session['url_back'] = url  # no error -> ok, remember this
                except Resolver404:
                    pass
                translation.activate(lg_curr)
                

                ...然后,在成功注册/登录后,如果有 req.session['url_back'] 然后我将其从会话中删除并对其进行重定向.

                ...and then later on, after successful registration/login, if there's a req.session['url_back'] then I remove it from session and make a redirect on it.

                这篇关于Django i18n_patterns:resolve() 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <i id='2tksW'><tr id='2tksW'><dt id='2tksW'><q id='2tksW'><span id='2tksW'><b id='2tksW'><form id='2tksW'><ins id='2tksW'></ins><ul id='2tksW'></ul><sub id='2tksW'></sub></form><legend id='2tksW'></legend><bdo id='2tksW'><pre id='2tksW'><center id='2tksW'></center></pre></bdo></b><th id='2tksW'></th></span></q></dt></tr></i><div id='2tksW'><tfoot id='2tksW'></tfoot><dl id='2tksW'><fieldset id='2tksW'></fieldset></dl></div>
                      <tbody id='2tksW'></tbody>

                      <small id='2tksW'></small><noframes id='2tksW'>

                    • <tfoot id='2tksW'></tfoot>

                      <legend id='2tksW'><style id='2tksW'><dir id='2tksW'><q id='2tksW'></q></dir></style></legend>

                        • <bdo id='2tksW'></bdo><ul id='2tksW'></ul>