Django删除结尾的斜杠-网址或重定向 [英] Django remove trailing slashes - urls or redirect

查看:114
本文介绍了Django删除结尾的斜杠-网址或重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除 url 中的结尾斜杠,或者如果在末尾添加了斜杠,则会进行重定向。我尝试使用Internet上的资源执行此操作,但是它对我不起作用。我想我做错了。



urls.py-应用

  app_name ='shop'
urlpatterns = [
#产品
path('',views.product_list,name ='product_list'),
path('show /< slug:slug>',views.product_show,name ='product_show'),
path('< slug:category>',views.product_list,name ='lst_by_ctgry' ),
path('< slug:category> /< slug:subcategory>',views.product_list,name ='lst_by_subctgry'),
path('< slug:category> //< ; slug:subcategory> /< slug:kind>',views.product_list,name ='lst_by_knds'),
#info-这些网址不能使用斜线不起作用。仅适用于斜杠
path('pad',views.pad,name ='pad'),
path('guarantee',views.guarantee,name ='guarantee'),
path('contacts,views.contacts,name ='contacts'),
path('about',views.about,name ='about'),
path('privacy',views。 privacy,name ='privacy')
]

urls.py-项目

  urlpatterns = [
path('',include('orders.urls')),
path('',include('cart.urls')),
path('',include('shop.urls')),
path('admin /',admin .site.urls),
]

settings.py

  APPEND_SLASH =假
REMOVE_SLASH =真实
MIDDLEWARE = [
...
'django.middleware.common.CommonMiddleware',
...
]

请给我一个例子,我如何删除所有URL中的尾部斜杠或获取重定向

解决方案

尽管建议使用斜杠。但是如果您不想这样做,可以在项目的 settings.py 文件中进行更改。



settings.py


APPEND_SLASH = False



I'm trying to remove the trailing slashes in my urls or make redirection happen if a slash is added at the end. I tried to do it on resources from the Internet but it does not work for me. I guess I'm doing something wrong.

urls.py - app

app_name = 'shop'
urlpatterns = [
    # product
    path('', views.product_list, name='product_list'),
    path('show/<slug:slug>', views.product_show, name='product_show'),
    path('<slug:category>', views.product_list, name='lst_by_ctgry'),
    path('<slug:category>/<slug:subcategory>', views.product_list, name='lst_by_subctgry'),
    path('<slug:category>/<slug:subcategory>/<slug:kind>', views.product_list, name='lst_by_knds'),
    # info - these urls doesn't work without slashes. works only with slashes
    path('pad', views.pad, name='pad'),
    path('guarantee', views.guarantee, name='guarantee'),
    path('contacts', views.contacts, name='contacts'),
    path('about', views.about, name='about'),
    path('privacy', views.privacy, name='privacy')
]

urls.py - project

urlpatterns = [
    path('', include('orders.urls')),
    path('', include('cart.urls')),
    path('', include('shop.urls')),
    path('admin/', admin.site.urls),
]

settings.py

APPEND_SLASH = False
REMOVE_SLASH = True
MIDDLEWARE = [
    ...
    'django.middleware.common.CommonMiddleware',
    ...
]

Сan you give an example please how can i remove trailing slashes in all urls or get redirect

解决方案

Though it's recommended to use the trailing slash. But if you don't want to have that you can change it in settings.py file of project.

settings.py

APPEND_SLASH = False

这篇关于Django删除结尾的斜杠-网址或重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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