在djang中创建并重定向到slug url

2024-05-08 00:03:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的应用程序上有医生的个人资料,并想为所有人创建slug网址。所以我有以下几个问题

  1. 如何为数据库中的新医生和现有医生创建slug url?在

url看起来像这样http://localhost:8000/docprofile/6/

我想把它做成这样http://localhost:8000/doctor/james-smith/

我要根据医生的名字做鼻涕虫

  1. 如何在实现slug url之后进行永久的url重定向,以便在某人转到http://localhost:8000/docprofile/6/时,页面被重定向到http://localhost:8000/doctor/james-smith/

这是模型.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)])

在视图.py在

^{pr2}$

在网址.py在

url(r'^docprofile/(?P<id>\d+)/$', views.showDocProfile, name='showDocProfile'),

Tags: namepyselflocalhosthttpurlmodels医生