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

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 %}

views.py

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

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

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

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%}

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.

1 Like

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 %}">

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

1 Like

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

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_]+)$']

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

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

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?

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.

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

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

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_]+)$']

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.

1 Like

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

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.

updated models.py (altered slug field)

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

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

# 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_]+)$']

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:

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.)

this is the url definition

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

<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>

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.

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

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

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

this is my views that brought to the p

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