Django如何添加斜杠 [英] How Django adds trailing slash

查看:54
本文介绍了Django如何添加斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django驱动的项目,该项目将自动在末尾添加斜杠.所以:

Working with the Django-powered project that would automatically put a trailing slash at the end. So:

foo.com/bar

将成为

foo.com/bar/

已阅读有一个 CommonMiddleware 类,该类具有一个 APPEND_SLASH 属性,通过该属性可以控制斜杠.我的项目确实导入了 CommonMiddleware :

I have read that there is a CommonMiddleware class which has an APPEND_SLASH attribute with which trailing slash can be controlled. My project import CommonMiddleware indeed:

MIDDLEWARE_CLASSES = (
    ...
    'django.middleware.common.CommonMiddleware',
)

但是 APPEND_SLASH 从未在任何地方设置.并且项目不断添加斜杠.

However APPEND_SLASH is never set anywhere. And the project keeps adding trailing slash.

问题:如果导入 CommonMiddleware APPEND_SLASH 是否默认设置为 True ?有什么方法可以控制斜杠?

Question: does APPEND_SLASH get set to True by default if you import CommonMiddleware? Are there any ways to control the trailing slash?

推荐答案

我将编译完整答案.它有两个方面:

I will compile a full answer. It has 2 aspects:

1)如果将 CommonMiddleware 导入到项目中,它将使用其 APPEND_SLASH 属性(即 True ).

1) If you import CommonMiddleware into your project - it would use its APPEND_SLASH attribute (which is True by default).

2)跟踪您的url文件(如果它是 ^ foo.com/bar/$ 之类的东西).

2) Keep track of your urls file (if it is something like ^foo.com/bar/$).

如果更改 CommonMiddleware APPEND_SLASH ,也不要忘记清除缓存(因为浏览器很可能会缓存它).

And don't forget to clear the cache if you change CommonMiddleware or APPEND_SLASH(since browser will most likely cache it).

这篇关于Django如何添加斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆