How to Use Dynamic Routing With Django

Without dynamic routing, navigating web pages would be difficult. You would have to type the full path of every page you visit on the browser. What a terrible user experience.

Dynamic Uniform Resource Locators (URLs), allow you to navigate to different pages on an app with the click of a button. Django makes it easy to design dynamic URLs. It has a URL Configuration module (URLconf) that connects URL expressions to views.

4

All code in the URLconf is in Python syntax, making it easy to create dynamic URLs. Let’s learn more about dynamic URLs by building a Django project.

1. Create a Django Project

First,create a Django projectand application (app).

Name your appBoma-watch. The app will collect location, description, and resident numbers from various neighborhoods. Create a model namedNeighborHood. Use the model to add information about the neighborhoods into a database. Learn how tocreate models in Djangoand the database if you are unfamiliar.

Dell monitor showing Windows 10 desktop

Next, create a view function for the neighborhoods.

2. Create a View Function

In Django, views are Python functions that take HTTP requests and return responses. On a web page powered by Django, views perform varied tasks and missions.

To execute a view, you must call it via a URL. A URL is a unique path to a resource on the web. The resource can be an HTML page, image, or an API endpoint.

Spark Mail app in window 11.

Create a URL that retrieves neighborhood data based on the parameters passed. To do so, you may use theprimary key (pk)orIdentification (id)to retrieve the information. You will use one template to do this.

You can create views by defining them in a file calledviews.pyin the app’s folder. Start by importing therenderfunction from Django to display data at the URL. Also import theNeighborHoodmodel frommodels.py.

mega cloud storage with mega logo on laptop screen.

Next, create a view function namedhomethat displays all neighborhoods on the home template. TheNeighborHood.objects.all()function receives data for all the neighborhoods from the database.

Also, create a view function forjoin_hoodthat will display a neighborhood’s information. TheNeighbourHood.objects.get(id=id)function requests data according to the ID. The information is then rendered on a template.

A blue circle with angled blue lines spreading out from the centre.

Later on, when you navigate to a neighborhood, you will be able to see its profile information.

3. Create a Dynamic URL

You can now create a dynamic URL for the view function you created.

Importpathfrom Django URLs to create paths for the view. Then import the view functions fromviews.py.

Thejoin_hoodURL has a placeholder in angle brackets:str:id. This captures that part of the URL and sends it to the view.

The angle brackets usually include a converter specification. The specification may either be a string(str) or integer (int). Django also offersslug, path, oruniversally unique identifiers(uuid). The specification limits the type of variable or number of characters passed in the URL to the view.

Giving the URLs a name will help identify them on the templates.

Thestaticandstaticfilesimports display static files on the URL paths. Learn more about how to structure URL parameters in the officialDjangodocumentation.

4. Add URL to a Template

Once you have added the view function to the URL, create an HTML template to display the data. You name the templatejoin_hood.html.

Create thejoin_hoodtemplate in the app folder to display the neighborhood data. First, extend thebase.htmltemplate with style sheets (bootstrap) that you will use to style the template. Then, render the variables that will display the information on the page.

Next, create ahome.htmltemplate where you will display all neighborhoods.

On the home page, render all the information you want to display about the neighborhoods. You will add a button and an anchor tag on the home page. The anchor tag has the URLnameandneighborhood ID.

When clicked, the button navigates to the neighborhood of that ID. The home template appears at the URL http://127.0.0.1:8000/home/. The picture below demonstrates this page:

5. Test Dynamic Routing

Now, you can test if the dynamic routing works on the template. When you click thejoin hoodbutton, It navigates to thejoin_hoodtemplate. Thejoin_hoodtemplate displays profile information about the neighborhood you selected.

You will also see the ID of the neighborhood displayed on the browser URL http://127.0.0.1:8000/join_hood/2/

It will appear as shown below:

Congratulations! You have created a dynamic URL.

Why Use Django URLs?

Dynamic URLs are an important feature in web applications. Django makes it easy to design URLs according to your needs. It removes the limitations you might associate with other frameworks.

The URLconf module is full of resources supporting Django URL creation. It also makes it easier to serve static files on templates and improves error handling.

Django has other features designed to optimize backend applications. It automates user authentication, content administration, and site maps, among other features.

Using this Django library, it’s possible to create a basic REST API in no time.

Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.

Flagship price, mid-range phone.

Make sure you don’t miss these movies and shows before Netflix removes them.

Some subscriptions are worth the recurring cost, but not these ones.

Your phone is a better editor than you give it credit for.

Technology Explained

PC & Mobile