Django的网址,不带后缀斜线不重定向(django urls without a trailin

2019-08-22 05:11发布

我有位于两个不同的计算机上的两个应用程序。 在计算机A上,在urls.py文件我有类似下面的一行:

(r'^cast/$', 'mySite.simulate.views.cast')

该网址将会为双方合作mySite.com/cast/mySite.com/cast 。 但是,计算机上的BI有写出来像一个类似的网址:

(r'^login/$', 'mySite.myUser.views.login')

对于在计算机B上的一些原因, url mySite.com/login /会工作,但mySite.com/login将挂起,并不会直接回mySite.com/login/喜欢它会在计算机A有什么我错过了什么? 这两个url.py文件看起来与我。

Answer 1:

检查你的APPEND_SLASH设置在settings.py文件

Django文档的详细信息



Answer 2:

或者,你可以写你的URL是这样的:

(r'^login/?$', 'mySite.myUser.views.login')

尾随斜线后的问题迹象使得它在正则表达式可选。 使用它,如果因为某些原因你不希望使用APPEND_SLASH设置。



Answer 3:

这提高了对@迈克尔Gendin的答案。 他的回答提供两个单独的网址相同的页面。 这将是最好有login自动重定向到login/ ,然后成为后者的主要页面:

from django.conf.urls import patterns
from django.views.generic import RedirectView

urlpatterns = patterns('',
    # Redirect login to login/
    (r'^login$', RedirectView.as_view(url = '/login/')),
    # Handle the page with the slash.
    (r'^login/', "views.my_handler"),
)


Answer 4:

我有同样的问题了。 (| /)我的正则表达式的端线前的我的解决办法是把。

url(r'^artists/(?P[\d]+)(|/)$', ArtistDetailView.as_view()),



Answer 5:

我有同样的问题。 在我的情况下,它是由一些老版本urls.py,从staticfiles前的陈旧遗留:

url(r'^%s(?P<path>.*)$' % settings.MEDIA_URL.lstrip('/'),
    'django.views.static.serve',
    kwargs={'document_root': settings.MEDIA_ROOT}),

MEDIA_URL是空的,所以这个模式匹配一​​切。



Answer 6:

附加斜线没有重定向 ,用它代替CommonMiddleware的设置,Django的2.1:

MIDDLEWARE = [
    ...
    # 'django.middleware.common.CommonMiddleware',
    'htx.middleware.CommonMiddlewareAppendSlashWithoutRedirect',
    ...
]

添加到您的主app目录middleware.py:

from django.http import HttpResponsePermanentRedirect, HttpRequest
from django.core.handlers.base import BaseHandler
from django.middleware.common import CommonMiddleware
from django.conf import settings


class HttpSmartRedirectResponse(HttpResponsePermanentRedirect):
    pass


class CommonMiddlewareAppendSlashWithoutRedirect(CommonMiddleware):
    """ This class converts HttpSmartRedirectResponse to the common response
        of Django view, without redirect.
    """
    response_redirect_class = HttpSmartRedirectResponse

    def __init__(self, *args, **kwargs):
        # create django request resolver
        self.handler = BaseHandler()

        # prevent recursive includes
        old = settings.MIDDLEWARE
        name = self.__module__ + '.' + self.__class__.__name__
        settings.MIDDLEWARE = [i for i in settings.MIDDLEWARE if i != name]

        self.handler.load_middleware()

        settings.MIDDLEWARE = old
        super(CommonMiddlewareAppendSlashWithoutRedirect, self).__init__(*args, **kwargs)

    def process_response(self, request, response):
        response = super(CommonMiddlewareAppendSlashWithoutRedirect, self).process_response(request, response)

        if isinstance(response, HttpSmartRedirectResponse):
            if not request.path.endswith('/'):
                request.path = request.path + '/'
            # we don't need query string in path_info because it's in request.GET already
            request.path_info = request.path
            response = self.handler.get_response(request)

        return response


文章来源: django urls without a trailing slash do not redirect