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

Good. What are those objects?

What that question is leading to, is:

If you have an object named p of type X that has a field named slug, how do you access the slug field of p?

i don’t really know how to put it, you know when you pass in a variable to context in django, and you want to loop throught it in a listing page, you have to run a {% for any_name in actual_variable %} so in my case i used p i could use either hat or banana or pants anything at all. i do not know if you at least understand what i am trying to say, i’m not really used to django terms that why i might be sounding confusing

Yes - you’re right on that.

You’re iterating over a list named prglangcat.

So I’m asking you to tell me what the data type is the objects in that list. (What model do they come from?)

sir, this is the model they are coming from

Ok, so if you have an object of type ProgrammingLanguagesCategory - lets call this object p - and you want to access the slug field of this object, how would you access it?

First i would write a view to get the slug

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

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

then in my template

prglangcat.slug

i don’t know if this is correct?
please don’t give up on me

No worries - we’ll work through this.

I’m not looking at a view or a template yet.

Lets start with this case:

Assume I write this statement, and that there is an instance of ProgrammingLanguagesCategory with an id = 1:

p = ProgrammingLanguagesCategory.objects.get(id=1)

I now have an object of type ProgrammingLanguagesCategory named p.

I want to print the title field of p:
print(...)

What would I put in the print statement to print the title field of p?

it’s going to be print(p.title)

Perfect.

Now then - how would you refer to a field named slug in that same object?

Finally then, that is what you need to use in your template to access the slug field in the url tag, if in your template you have an object named p that is referring to an instance of ProgrammingLanguagesCategory.

1 Like

Finnaly it’s works
Thanks alot for your time, i do appreciate it