i want to add slug in url using django like this <a href="{% url 'base:tutorial-page' p.slug p.slug2 %}" </a>
i dont really know how to pass in double slug in the url for example: i want to access the html page before going to the tutorial page related to html.
getting-started.html
{% for p in prglangcat %}>
{{ p.title }}
<a href="{% url 'base:tutorial-page' p.slug p.slug %}" </a>
{% endfor %}
strong text
def gettingStarted(request):
prglangcat = ProgrammingLanguagesCategory.objects.all()
context = {
'prglangcat': prglangcat
}
return render(request, 'base/getting-started.html', context)
def programmingLanguageTutorial(request, prg_slug):
prglangcat = ProgrammingLanguagesCategory.objects.get(slug=prglangcat_slug)
prglangtut = ProgrammingLanguageTutorial.objects.get(slug=prg_slug, prglangcat=prglangcat)
context = {
'prglangtut': prglangtut
}
return render(request, 'base/tutorial-page.html', context)
models.py
class ProgrammingLanguagesCategory(models.Model):
title = models.CharField(max_length=100)
icon = models.ImageField(upload_to='programming-category', default="default.jpg")
description = models.TextField(default="Learn ...")
slug = models.SlugField(max_length=100, unique=True)
def get_absolute_url(self):
return reverse('programming-languages-category', args=[self.slug])
def __str__(self):
return self.title
class ProgrammingLanguageTutorial(models.Model):
prglangcat = models.ForeignKey(ProgrammingLanguagesCategory, on_delete=models.CASCADE, null=True)
slug = models.SlugField(max_length=10000, unique=True, null=True)
title = models.CharField(max_length=10000)
description = models.TextField(null=True)
image = models.ImageField(upload_to='Tutorial Image', default="default.jpg", null=True)
code_snippet = models.CharField(max_length=1000000000, null=True, blank=True)
video_url = models.URLField(null=True)
views = models.IntegerField(default=0)
def __str__(self):
return self.title
urls.py
app_name = 'base'
urlpatterns = [
path('', views.index, name="index"),
path('getting-started/', views.gettingStarted, name="getting-started"),
path('getting-started/<slug:prglangcat_slug>/<slug:prg_slug>', views.programmingLanguageTutorial, name="tutorial-page"),
]
traceback
NoReverseMatch at /getting-started/
Reverse for 'tutorial-page' with no arguments not found. 1 pattern(s) tried: ['getting\\-started/(?P<prglangcat_slug>[-a-zA-Z0-9_] )/(?P<prg_slug>[-a-zA-Z0-9_] )$']
CodePudding user response:
You are not passing in the second argument in:
def programmingLanguageTutorial(request, prg_slug):
CodePudding user response:
You can use like keyword arguments and its accepted the same way, and its more explicit which argument is which.
Do like this:
<a href="{% url 'base:tutorial-page' prg_slug=p.slug prglangcat_slug=p.slug2 %}" </a>