代码之家  ›  专栏  ›  技术社区  ›  strumpy_strudel

使用regex创建catch all url()条目

  •  4
  • strumpy_strudel  · 技术社区  · 7 年前

    所以我的django没有任何问题,在dev中也没有反应URL路由,但是现在我正试图转移到生产环境中,遇到了各种各样的问题。

    是的,我讨厌雷杰克斯。它看起来像一只猫在键盘上行走。当然,我需要坐下来致力于学习。

    在Dev中,我的“捕获”只是以下几点,它们非常有效:

    url(r'', TemplateView.as_view(template_name='index.html')),
    

    在生产中,我得到 Uncaught SyntaxError: Unexpected token < .正如我所解释的,这与JS在URL中被捕获有关,而不是 index.html JS需要“通过”。我被告知要尝试:

    url(r'^$', TemplateView.as_view(template_name='index.html')),
    

    这奏效了。Web应用程序已加载,我可以导航。

    然而,当涉及到验证邮件链接时,又出现了另一个问题。我遇到了一些问题 Page not found (404) 同样,这在我的开发人员设置中不是问题。

    电子邮件链接如下:

    https://test.example.com/auth/security_questions/f=ru&i=101083&k=6d7cd2e9903232a5ac28c956b5eded86c8cb047254a325de1a5777b9cca6e537

    我得到的是:

    Page not found (404) Requested URL: http://test.example.com/auth/security_questions/f%3Dru&i%3D101083&k%3D6d7cd2e9903232a5ac28c956b5eded86c8cb047254a325de1a5777b9cca6e537/

    我的反应路线如下:

    <App>
        <Switch>
            <Route exact path='/auth/security_questions/f=:f&i=:id&k=:key' component={SecurityQuestions} />
            <Route exact path='/auth/*' component={Auth} />
            <Route exact path='/' component={Auth} />
        </Switch>
    </App>
    

    这应该是 /auth/security_questions/... 路线。

    我的 urls.py 如下所示:

    urlpatterns = [
        # API authentication entry point   
        url(r'^api/auth/', include('authentication.urls', namespace='signin')),
    
        # Any requets that come through serve the index.html
        # url(r'^$', TemplateView.as_view(template_name='index.html')),
    
    ] + static(settings.STATIC_URL, 
    document_root=settings.STATIC_ROOT)
    

    此外,还有 authentication.urls 以下内容:

    urlpatterns = [
        url(r'^security_questions/', SecurityQuestionsAPIView.as_view(), name='security_questions'),
    ]
    

    似乎Django正在尝试处理路由,显然没有匹配的路由,而实际上应该只呈现 index.html文件 react-router-dom 接管从FE向API发送请求。因此,似乎我需要抓住让JS通过的所有方法。

    我遇到了一个似乎相关的问题: react routing and django url conflict .所以我添加了以下内容以便 / 抓住然后“其他一切”抓住所有。

    # match the root
    url(r'^$', TemplateView.as_view(template_name='index.html')),
    # match all other pages
    url(r'^(?:.*)/?$', TemplateView.as_view(template_name='index.html')),
    

    仍然不会呈现验证链接。为最后一个“全部捕获”URL尝试了其他一些变体:

    Django route all non-catched urls to included urls.py

    url(r'^', TemplateView.as_view(template_name='index.html')),

    结果 未捕获语法错误:意外标记<

    django catching any url?

    url(r'^.*', TemplateView.as_view(template_name='index.html')),

    见前面。

    所以我们要深入研究姜戈,雷杰克斯,并尝试解决这个问题,但同时…

    我在这里做错什么了?

    1 回复  |  直到 7 年前
        1
  •  1
  •   strumpy_strudel    7 年前

    好的,就快到了。我修改了:

    url(r'^(?:.*)/?$', TemplateView.as_view(template_name='index.html')),

    为此:

    url(r'^(?:.*)/$', TemplateView.as_view(template_name='index.html')),

    它阻止了 Uncaught SyntaxError: Unexpected token < 错误。它将加载Web应用程序的部分内容,但不是整个应用程序。这个问题是由于URL编码,所以我必须清理我的URL格式。我在这里有个问题:

    Prevent URL encoding that is removing equals signs from URL

    现在一切似乎都装好了。