我有一个医生数据库,我想改变他们的url结构:
当前url为:localhost:8000/docprofile/32/
localhost:8000/doctor/john-doe/
<标题>新views.py h1> urls . py h1> 成功地更改了url。我的问题是我如何做一个永久的301 url重定向,这样,如果有人访问localhost:8000/docprofile/32/
它重定向到localhost:8000/doctor/john-doe/
?
将这些添加到您的文件中。
Views.py
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
def showDocProfileOld(request, id):
doctor = get_object_or_404(Doctor, id=id)
return HttpResponseRedirect(reverse('showDocProfile', args=[doctor.slug]))
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'),
url(r'^docprofile/(?P<id>d+)/$', views.showDocProfileOld, name='showDocProfileOld'),