# django: how to add slug as arguments in url tag using django

**URL:** <https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636>\
**Category:** Forms & APIs\
**Created:** [March 16, 2022, 9:06am UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636 "2022-03-16T09:06:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 9:06am UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/1 "2022-03-16T09:06:14Z")

</div>

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

```auto
{% for p in prglangcat %}>
      {{ p.title }}
      <a href="{% url 'base:tutorial-page' p.slug p.slug %}" </a>
{% endfor %}

```

views.py

```auto
def gettingStarted(request):
    prglangcat = ProgrammingLanguagesCategory.objects.all()
    context = {
        'prglangcat': prglangcat
    }
    return render(request, 'base/getting-started.html', context)

def programmingLanguageTutorial(request, prg_slug, prglangcat):
    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

```auto
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)
    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

```auto
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"),
]

```

taceback

```auto
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_]+)

```

what i have tried  
`{% url 'base:tutorial-page' prg_slug=p.prg_slug prglangcat_slug=p.prglangcat_slug%}`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 12:53pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/2 "2022-03-16T12:53:24Z")

</div>

The first thing to verify is that you don’t have any instances of either a `ProgrammingLanguagesCategory` or `ProgrammingLanguageTutorial` object with a blank `slug` field. That’s the most common cause of this type of issue. You can examine your database to determine that.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 4:49pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/3 "2022-03-16T16:49:38Z")

</div>

i checked my database now and it i do not have any blank `slug` field, actually when i access the page by manually typing the url in the url search bar it works fine, now what i want is to click a button to get to the page rather than manually type the url. and it requesting a slug argument and i do not really know to pass in slug arguments in in `<a href="{% url 'base:tutorial-page' now_slug_should_be_here and another_slug_here %}">`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 5:08pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/4 "2022-03-16T17:08:02Z")

</div>

You have at least one instance of `ProgrammingLanguagesCategory` with a null slug field.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 6:40pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/5 "2022-03-16T18:40:02Z")

</div>

okat that’s true and i just found and remove the `null=True`  
please how does this affect the slug that i am trying to pass in the url

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 6:42pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/6 "2022-03-16T18:42:22Z")

</div>

okat this is the new url with slug arguments, `{% url 'base:tutorial-page' p.prg_slug p.prglangcat_slug%}"` and this is the **current error** `Reverse for 'tutorial-page' with arguments '('', '')' not found. 1 pattern(s) tried: ['getting\\-started/(?P<prglangcat_slug>[-a-zA-Z0-9_]+)/(?P<prg_slug>[-a-zA-Z0-9_]+)$']`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 6:44pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/7 "2022-03-16T18:44:14Z")

</div>

This means you still have at least one slug field that is blank. (Or a 0-length string - effectively the same thing here.)

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 6:47pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/8 "2022-03-16T18:47:38Z")

</div>

please help me look into it maybe there is something i am not seeing, this is all my models

```auto
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)
	title = models.CharField(max_length=10000)
	description = models.TextField(null=True)
	image = models.ImageField(upload_to='Tutorial Image', null=True, blank=True)
	code_snippet = models.CharField(max_length=1000000000, null=True, blank=True)
	video_url = models.URLField(null=True, blank=True)
	views = models.IntegerField(default=0)

	def __str__ (self):
		return self.title

```

or do i need to delete my database and run makeigrations and migrate again?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 6:48pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/9 "2022-03-16T18:48:36Z")

</div>

There is nothing identifiably wrong with your code, or your models. It’s your data. You have at least one object that has a blank slug field.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 6:49pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/10 "2022-03-16T18:49:25Z")

</div>

okay let me check my data base again or rather i would delete the whole database and run it again.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 6:55pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/11 "2022-03-16T18:55:02Z")

</div>

i just deleted my database and ran it again, when i had no data saved, the page seems to the working fine, then i added a category and it threw the error again, but when i remove the the value that are in the url here `<a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug%}"> ` it also works

traceback

```auto
NoReverseMatch at /getting-started/
Reverse for 'tutorial-page' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['getting\\-started/(?P<prglangcat_slug>[-a-zA-Z0-9_]+)/(?P<prg_slug>[-a-zA-Z0-9_]+)$']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 6:57pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/12 "2022-03-16T18:57:46Z")

</div>

Sorry, if there’s still an issue here I’m not following you.

If you’re still having a problem, please post the current model, view, template and url definition that is causing the error. Do not post information about what has worked or other things that you have tried - that just gets confusing.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 7:08pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/13 "2022-03-16T19:08:25Z")

</div>

I have updated the question with the current model, view template and url as you said

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 7:12pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/14 "2022-03-16T19:12:38Z")

</div>

I’m sorry, I can’t tell what you have and haven’t updated at the top.

Please repost down here what the current code and error messages are.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 7:16pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/15 "2022-03-16T19:16:58Z")

</div>

updated models.py (altered slug field)

```auto
from django.db import models
from django.contrib.auth.models import User
import uuid

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(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(unique=True)
	title = models.CharField(max_length=10000)
	description = models.TextField(null=True)
	image = models.ImageField(upload_to='Tutorial Image', null=True, blank=True)
	code_snippet = models.CharField(max_length=1000000000, null=True, blank=True)
	video_url = models.URLField(null=True, blank=True)
	views = models.IntegerField(default=0)

	def __str__ (self):
		return self.title

```

**url tag in getting-started.html**  
`<a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug slug=p.prg_slug %}" >`

views.py

```auto
def index(request):
	prglangcat = ProgrammingLanguagesCategory.objects.all()
	context = {
		'prglangcat': prglangcat
	}
	return render(request, 'base/index.html', context)

def gettingStarted(request):
	prglangcat = ProgrammingLanguagesCategory.objects.all()
	context = {
		'prglangcat': prglangcat
	}
	return render(request, 'base/getting-started.html', context)

def programmingLanguageTutorial(request, prg_slug, prglangcat_slug):
	prglangcat = ProgrammingLanguagesCategory.objects.get(slug=prglangcat_slug)
	prglangtut = ProgrammingLanguageTutorial.objects.get(slug=prg_slug, prglangcat=prglangcat)
	prglangcat = ProgrammingLanguagesCategory.objects.all()

	context = {

		'prglangtut': prglangtut,
		'prglangcat': prglangcat
	}
	return render(request, 'base/tutorial-page.html', context)

```

traceback

```auto
# NoReverseMatch at /getting-started/
Reverse for 'tutorial-page' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['getting\\-started/(?P<prglangcat_slug>[-a-zA-Z0-9_]+)/(?P<prg_slug>[-a-zA-Z0-9_]+)$']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 7:22pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/16 "2022-03-16T19:22:10Z")

</div>

Ok, need to see the url definition and more of `getting-started.html`. (If that anchor tag is inside a loop, I’ll need to see the complete loop. If _that_ loop is inside another loop, I’ll need to see that outer loop, too.)

From what I can see so far:

> [@desphixs](#):
>
> `<a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug slug=p.prg_slug %}" >`

You’re trying to assign to the same url parameter twice. (Without seeing that url definition, I can’t tell what those parameter references should be.)

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 7:26pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/17 "2022-03-16T19:26:49Z")

</div>

this is the url definition

```auto
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"),
]

```

the complete loop

```auto
<div class="row">
    {% for p in prglangcat %}
    <div class="col-md-3">
        <div class="stream_1 mb-30">
            <a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug slug=p.prg_slug%}" class="stream_bg">
                <img src="{{ p.icon.url }}" alt="" />
                <h4><b>{{p.title}}</b></h4>
                <p>{{p.description}}<span></span></p>
            </a>
        </div>
    </div>
    {% endfor %}
</div>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 7:31pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/18 "2022-03-16T19:31:18Z")

</div>

> [@desphixs](#):
>
> ```auto
> path('getting-started/<slug:prglangcat_slug>/<slug:prg_slug>', views.programmingLanguageTutorial, name="tutorial-page"),
> ]
> 
> ```

You can provide the two parameters in one of two ways.

You can either just provide them positionally:  
`{% url 'tutorial-page' s1 s2 %}`  
where s1 and s2 are the parameters to be substituted into the url.

Or you can provide them with keywords - in this case, the keywords are `prglangcat_slug` and `prg_slug`, creating:  
`{% url 'tutorial-page' prglangcat_slug=s1 prg_slug=s2 %}`

However, you had posted that you have:  
`<a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug slug=p.prg_slug %}" >`  
which is not correct.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2022, 7:32pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/19 "2022-03-16T19:32:50Z")

</div>

Aside from the parameter issue, regarding the loop in the template, you have:

> [@desphixs](#):
>
> `<a href="{% url 'base:tutorial-page' slug=p.prglangcat_slug slug=p.prg_slug%}" class="stream_bg">`

What is the data type of `p` here? (What is it an instance of?)

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [March 16, 2022, 7:37pm UTC](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636/20 "2022-03-16T19:37:16Z")

</div>

i’m using `p` to access each of the objects i’m looping through

this is my views that brought to the **p**

```auto
def gettingStarted(request):
	prglangcat = ProgrammingLanguagesCategory.objects.all()
	context = {
		'prglangcat': prglangcat
	}
	return render(request, 'base/getting-started.html', context)

```

so what i did was `{% for p in prglangcat %}` **NOTE** `p` can be named anything i’m just using it as a name to loop through the objects i have in my database.  
**NOTE AGAIN:** The `<a href="{% ... %}"` is inside the loop

[Next page](https://forum.djangoproject.com/t/django-how-to-add-slug-as-arguments-in-url-tag-using-django/12636.md?page=2)
