我有一個診療模式,目前他們的鏈接看起來像這樣的Django 1.6:創建蛞蝓網址
localhost:8000/clinic/1/
我想讓他們看起來像這樣
localhost:8000/clinic/Nice-Medical-Clinic/
我想蛞蝓是診所
這裏的名稱是models.py
class Clinic(models.Model):
name = models.CharField(max_length=500)
email = models.EmailField(blank = True, null = True)
address = map_fields.AddressField(max_length=200
website = models.CharField(max_length=50, blank = True, null = True)
submitted_on = models.DateTimeField(auto_now_add=True, null = True, blank = True)
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('meddy1.views.clinicProfile', args=[str(self.id)])
這裏是views.py
def clinicProfile(request, slug, id):
clinic = Clinic.objects.get(id=id)
doctors = Doctor.objects.all().order_by('-netlikes')
d = getVariables()
d.update({'clinic': clinic, 'doctors': doctors, })
return render(request, 'meddy1/clinicprofile.html', d)
urls.py
url(r'^clinic/(?P<id>\d+)/$', views.clinicProfile, name='clinicProfile'),
感謝@cor。這隻適用於我補充的新診所。現有的診所呢?我怎樣才能讓現有的診所有一個slu as的診所名稱? – 2014-09-12 10:55:01
我不知道最好的辦法。我會從shell手動執行它。一旦你做出了改變並遷移de db,就像是:'clinics = Clinic.objects.all()'然後'在診所就診:clinic.slug = slugify(clinic.name,instance = clinic)' – cor 2014-09-12 10:59:26
這是實際上比這更容易。診所= Clinic.objects.all()'然後'在診所診所:clinic.save()'。另外請記住,診所的名稱必須是唯一的。你不能將同一個網址映射到兩個不同的診所。 – Jonathan 2014-09-12 11:10:45