Hi, welcome back.
Previously, we built the homepage of our
app showing a list of Blog Posts.
Now we want to add a link, that will
take the user to each of the Blog Post.
So let's do that.
We have an h1 tag, HTML tag and a p
tag, so paragraph tag, which are in index.html.
So, what we could do is that, instead of
having this as simple text, we can turn it
into a link, so the titles could become links.
To do that, instead of using h2 tags, we
could use a tags, and put the title in between.
Now, a tags in HTML are used for links and
buttons, so clickable elements, HTML elements, and therefore these
tags, expect to have a link which should come
as a value of the href property.
So this as you see, goes inside these brackets.
So a, the first a is this one in here, then
comes the title, then comes the second a, with this slash.
So href is equal to
parentheses, and inside the parentheses
now, we want to insert the link to the Blog Post.
So we want to make this dynamic right.
We are here inside a for loop, that is
looping through every post, in our post_list.
And so we have access to each
of the posts, through this variable.
post is an instance.
So in the first iteration this will be the Cat post.
Therefore, so here we get the title of Cats,
but then we can get the slug of cats.
So which could be something, like localhost:8000
and then we could say post.slug.
So this would be one way to do it.
So post.slug would be variable, this would be
one way to do it, but it's not the
best way because, localhost will be changed to something
else, probably later on, when you deploy your app.
And the best way to do it is, to delete this.
And inside this parenthesis, you use a
URL tag, which comes with this symbol.
So curly bracket, percentage, percentage, curly bracket and inside
here you say url, so it's a keyword.
And inside single quotes this time,
or you can also use double quotes, but
since we used double quotes here, it makes
sense to use single quotes here.
Inside here, goes the url of that current
post, that is being iterated in the loop.
So we need the URL of the post,
which we can get from urls.py.
So we're talking about this url, right?
So blog_view.
So this name that we gave to this
urlpattern, is now being used, in here,
to extract that particular URL pattern.
So, that was again blog_view, blog_view.
Let's save this and see what's going to happen.
Save, make sure the server is running
go here, reload, and we get this error, Reverse
for blog_view, with no arguments.
So this expects a slug argument,
[No Audio]
this url tag here.
So this blog_view also needs to get
[No Audio]
post without quotes .slug.
So the slug of the post, of that
current post, save, reload, and this time we
see these are being rendered as links.
So let's click one of them Cats, and
it takes us to the cats web page.
Dogs and it takes us to the dogs web page.
Now we can improve the looking of this by
putting this thing inside, some h2 tags.
So that entire variable including the curly brackets
goes inside h2 tags, and reload.
Yeah, it looks better now.
And that is how, you use URL tags in Django.
That is the format.
Thanks for following this video.
In the next video, we are going to
use some styling using the Bootstrap CSS library.
Talk to you later.