[Django]-How to add multiple slugs into one url path in Django 2.1?

3๐Ÿ‘

โœ…

I had the same problem, you can store and pass your first slug slug:studylevelslug(use session or save it as a fields in your model) then pass multi slug to your url path same as you did in your code :
path('study/<slug:studylevelslug>/<slug:subjectslug>/', views.SubjectDetailView.as_view(), name='subject-detail'),
in your template when you call {% url %} pass your slugs like this :

{% url 'subject-detail' studylevelslug=YOURFIRSTSLUG subjectslug=YOURSECONDSLUG  %}

I used this in my project and it worked completely fine.
for more detail about how store your slug in session use this link:
How use session in Django

if you need any further help, ask and will happy to help.

๐Ÿ‘คAli Akhtari

0๐Ÿ‘

A DetailView is for a single object so you cannot have it with 2 models.

Try something like this:

class SubjectDetailView(generic.DetailView):
    model = Programmesearch
    template_name = 'mnsdirectory/subject_detail.html'
    slug_field = 'studylevelslug'
    slug_url_kwarg = 'studylevelslug'

    def get_study_level(self, *args, **kwargs):
        return StudyLevel.objects.get(slug=subjectslug)

    def get_context_data(self, *args, **kwargs):
        ctx = super().get_context_data(*args, **kwargs)
        ctx['study_level'] = self.get_study_level()
        return ctx

Youโ€™ll now have access to object and study_level within your template. object will hold your Programmsearch data and study_level your StudyLevel data

๐Ÿ‘คHenryM

Leave a comment