我正在尝试创建重定向规则:
来自:localhost:8000/api/v1/users/.json
致:localhost:8000/api/v1/users.json
我实际上得到了什么:
http://localhost:8000/api/v1/users/.json/users.json
这是我的全局网址文件:
from django.conf.urls import include, patterns, url
from django.contrib import admin
urlpatterns = patterns(
'',
url(r'^api/v1/', include('api.urls', namespace='v1')),
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^docs/', include('rest_framework_swagger.urls')),
)
这是我的应用专用网址文件:
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
from rest_framework.urlpatterns import format_suffix_patterns
from api import views
urlpatterns = patterns(
'',
# Redirect rule
url(r'^(?P<url>.*)/.json/$', RedirectView.as_view(
url='%(url)s.json',
permanent=True)),
# Users
url(r'^users/$', views.UserList.as_view()),
url(r'^users/(?P<id>[0-9]+)/$', views.UserDetail.as_view()),
# Sessions
url(r'^sessions/$', views.SessionList.as_view()),
url(r'^sessions/(?P<id>[0-9]+)/$', views.SessionDetail.as_view()),
# Password Resets
url(r'^users/(?P<id>[0-9]+)/reset_password/$',
views.ResetPasswordDetail.as_view()),
# Email Confirmations
url(r'^users/(?P<id>[0-9]+)/confirm_email/$',
views.ConfirmEmailDetail.as_view()),
# Demographics
url(r'^users/(?P<id>[0-9]+)/demographics/$',
views.DemographicsDetail.as_view()),
)
urlpatterns = format_suffix_patterns(urlpatterns)
我做错了什么?
答案 0 :(得分:2)
您在url
的开头缺少斜线。我还建议使用比.*
更具体的模式,例如\w+
。您也可以从正则表达式中删除尾部斜杠。
url(r'^(?P<url>\w+)/.json$', RedirectView.as_view(
url='/%(url)s.json',
permanent=True)),
最后,最好先使用permanent=False
进行测试,直到您确定它正常工作,以阻止浏览器缓存不正确的重定向。