You are here

Dynamic Links in Django

Introduction

Let us take an example of the blog post entries on a web page. If there are lot many blog posts, it would become very difficult to scroll up and down to read a post. Instead, it would be much better and aneasier way of accessing the post we could for examle pass the blog id in url e.g. localserver:8000/1 or localserver:8000/4 etc. This will directly take you to bolog id ‘1’ or blog id ‘4’.

This is also the case with twitter or facebook etc when people say follow me @name. Since there are huge number f users and username were displayed on the page to select from, it would not be possible for us to querry/serch and access the page. But by using dynamic links written as www.facebook.com/user_name etc, make querry a very simple one.

Let us do it:

step-1: in the views.py create another link say posts and define it as :

def posts(request, pk_test): # argument pk_test is say primary key

post=blogDataDb.objects.get(id=pk_test) # tis should be provided in webpage

return render(request, ‘posts.html’, {‘post’ : post})

but, you may be wondering how and from where to get the parameter pk_test as an argument in def posts() function.

step-2: in the urls.py

Well, this is to be provided in the url, and hence in the url.py

url.py

from . Import views

urlpatterns =[

path(‘’, views.home, name= ‘home’)

path(‘delete/<list_id>’, views.home, name= ‘home’)

path(‘posts/<str : pk_test>/’, views.posts, name= ‘posts’)

]

remember to add a backslash after pk_test otherwise you may get error.

So whatever parameter (blog_id) you provide in the url (e.g. localhost:8000/formApp/posts/1/ ) will get into pk_test here and then becomes argument in views.posts function def posts(request, pk_test) and into post=blogDataDb.objects.get(id=pk_test) in the views.py.

Finally the return statement then takes it on the webpage.

Step-3: create posts.html file

here we have to display only one instance with id as pk_test on this webpage, so we don’t need a for loop

<body>

<h1>Click the link you want to access</h1>
<h2> The post id is : {{blogitems.id}}</h2>
<h3>{{ blogitems.title }}</h3>
<h4>post written by : {{ blogitems.author }}</h4>
<p>{{blogitems.contents}}</p>

</body>

Now run the Django server and open the application in browser. Try pass parameter as post id and you will see post for that id appearing on the same page.

Remeber to write url as localhost:8000/posts/1 # ‘i’ is the post id=1. where localhost is https://127.0.0.1

 

Step-5

finally give links to titles in home page (index.html)

Now instead of typing url parameters every time, why not to have clickable links for post titles on the home page, so that when the link is clicked then it take us to posts.html page and display the posts from the database. remember the contents on the posts.html are not static contents rather it accesses posts from the database for that link.

Easiest way is to put all titles in a for loop in the posts.html page as:

{% for allposts in all_posts %}

<a href=“{% url ‘posts’ allposts.id %}”>{{allposts.title}}</a> <br>

{% endfor %}

Now Refresh the browser https://127.0.0.1:8000/formApp and then click on any link to see the post appearing on another page as seen in figure

Leave a Reply

Top
error: Content is protected !!