我想有一个这样的URL路径:
www.example.com/学士/frankfurt-university/corporate-finance
如您所见,在本例中,URL路径包括3个slug。
我有三个不同的班级。弹头分别包含在每个弹头内。我需要做什么才能实现这种类型的URL路径?我可以用一个slug创建URL路径,但不能用多个slug。
每一个帮助或想法都是高度赞赏的。我找不到任何来源。
网址.py
app_name = 'mnsdirectory'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'), #views.index
path('study/<slug:studylevelslug>/<slug:subjectslug>/', views.SubjectDetailView.as_view(), name='subject-detail'),
path('study-abroad/<slug:studylevelslug>/', views.StudylevelDetailView.as_view(), name='studylevel-list'),
]
视图.py
class IndexView(generic.ListView):
model = Programmesearch
template_name = 'mnsdirectory/index.html'
context_object_name = 'universities'
queryset = Programmesearch.objects.all()[:6]
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['studylevels'] = StudyLevel.objects.all()[:10]
return context
class SubjectDetailView(generic.DetailView):
model = Programmesearch
template_name = 'mnsdirectory/subject_detail.html'
slug_field = 'subjectslug'
slug_url_kwarg = 'subjectslug'
class StudylevelDetailView(generic.DetailView):
model = StudyLevel
template_name = 'mnsdirectory/study_level.html'
slug_field = 'studylevelslug'
slug_url_kwarg = 'studylevelslug'
模型.py
class Programmesearch(models.Model):
study_country = models.CharField(max_length=100,
choices=COUNTRY_CHOICE)
full_subject_name = models.CharField(max_length=100, blank=True,
null=True)
def get_unique_slug(self):
subjectslug = slugify(self)
unique_slug = subjectslug
counter = 1
while mnsdirectory.objects.filter(subjectslug = unique_slug).exists():
unique_slug = '{}-{}'.format(subjectslug, counter)
counter += 1
return unique_slug
subjectslug = models.SlugField(unique=True)
def __str__(self):
return self.subjectslug
def get_absolute_url(self):
return reverse('SubjectDetailView', args=[str(self.id)])
def pre_save_programmesearch_receiver(sender, instance, *args, **kwargs):
subjectslug = slugify(instance.full_subject_name)
exists = Programmesearch.objects.filter(subjectslug=subjectslug).exists()
if exists:
subjectslug = "%s-%s" %(subjectslug, instance.id)
instance.subjectslug = subjectslug
pre_save.connect(pre_save_programmesearch_receiver, sender=Programmesearch)
class StudyLevel(models.Model):
title = models.CharField(max_length=100, blank=True, null=False)
studylevelslug = models.SlugField(unique=True, editable=False, max_length=100)
def __str__(self):
return self.studylevelslug
def get_absolute_url(self):
return reverse('StudylevelDetailView', args=[str(self.id)])
def get_unique_slug(self):
studylevelslug = slugify(self.study_level)
unique_slug = studylevelslug
counter = 1
while StudyLevel.objects.filter(studylevelslug = unique_slug).exists():
unique_slug = '{}-{}'.format(studylevelslug, counter)
counter += 1
return unique_slug
def pre_save_studylevel_receiver(sender, instance, *args, **kwargs):
studylevelslug = slugify(instance.study_level)
exists = StudyLevel.objects.filter(studylevelslug=studylevelslug).exists()
if exists:
studylevelslug = "%s-%s" %(studylevelslug, instance.id)
instance.studylevelslug = studylevelslug
pre_save.connect(pre_save_studylevel_receiver, sender=StudyLevel)