我的应用程序上有医生个人资料,并希望为所有人创建slug网址。所以我提出了以下问题
网址看起来像http://localhost:8000/docprofile/6/
我希望这样做http://localhost:8000/doctor/james-smith/
我希望slu is基于医生的名字
http://localhost:8000/docprofile/6/
时,该网页会重定向到http://localhost:8000/doctor/james-smith/
这是models.py
class Doctor(models.Model):
name = models.CharField(max_length=1300)
specialization = models.ForeignKey(Specialization)
def __unicode__(self):
return u"%s %s" % (self.name, self.specialization)
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('kk1.views.showDocProfile', args=[str(self.id)])
views.py
def showDocProfile(request, id):
doctor = Doctor.objects.get(id=id)
clinic = Clinic.objects.get(id=doctor.clinic.id)
d = getVariables(request,dictionary={'page_name': doctor.name +" "+ doctor.specialization.name +" -})
d.update({'doctor': doctor, 'clinic': clinic,'form': form })
return render(request, 'kk1/docprofile.html', d)
urls.py
url(r'^docprofile/(?P<id>\d+)/$', views.showDocProfile, name='showDocProfile'),
答案 0 :(得分:1)
第一个问题:
url(r'^doctor/(?P<name>\w+)/$', views.showDocProfileByName),
第二个问题:
def showDocProfileById(request, id):
doctor = Doctor.objects.get(id=id)
name = doctor.name.replace(' ', '-')
HttpResponseRedirect(reverse('project.views.showDocProfileByName', args=(name)))
答案 1 :(得分:0)
您可以让用户输入身份证或医生姓名
url(r'^docprofile/(?P<identifier>(\d+|\w+))', views.showDocProfile, name='showDocProfile')