【发布时间】:2016-01-08 05:24:34
【问题描述】:
我有一个医生数据库,我想更改他们的 url 结构:
当前网址为:localhost:8000/docprofile/32/
旧views.py
def showDocProfile(request, id):
doctor = get_object_or_404(Doctor, id=id)
d = getVariables(request,dictionary={'page_name': "Dr." + doctor.name+"" })
d.update({'doctor': doctor, 'doctors': Doctor.objects.all()})
return render(request, 'm1/docprofile.html', d)
旧 urls.py
url(r'^docprofile/(?P<id>\d+)/$', views.showDocProfile, name='showDocProfile'),
新的网址是localhost:8000/doctor/john-doe/
新的views.py
def showDocProfile(request, slug):
doctor = get_object_or_404(Doctor, slug=slug)
d = getVariables(request,dictionary={'page_name': "Dr." + doctor.name+"" })
d.update({'doctor': doctor, 'doctors': Doctor.objects.all()})
return render(request, 'm1/docprofile.html', d)
新的 urls.py
url(r'^doctor/(?P<slug>[\w-]+)/$', views.showDocProfile, name='showDocProfile'),
我成功更改了网址。
我的问题是如何进行永久的 301 url 重定向,这样如果有人访问 localhost:8000/docprofile/32/,它就会被重定向到 localhost:8000/doctor/john-doe/?
【问题讨论】:
-
保留旧网址和旧视图,每当有请求时,请从旧视图重定向到新网址。
-
@GeoJacob 我该怎么做?我对 Django 很陌生。
标签: python django url redirect