In any application or project that you work on, having route paths or urls adds an extra level of functionality to improve the user experience. Being able to guide the user from place to place with navigation makes it easier for the creator to have the user see what they want the user to see. In React, there is React Router, in Rails there is the ‘routes.rb’ file. Today, I’m diving into how to configure urls in Django.

Before getting into it, I do use the terms ‘route’, ‘path’, and ‘url’ pretty interchangeably. They are pretty much the same thing. The url address determines where to go in the project which then displays something to the user. Don’t get too confused.

A URL (Uniform Resource Locator) commonly referred to as a web address. Is whatever is displayed in your browsers web address bar. Whatever is typed or displayed represents a path that the user (that’s you) is trying to get to. If that path exists, what ever the path results in will come up on your screen. Maybe it’s the login page, or where you can edit your profile, or write that amazing blog post. If you build your own project that is larger than a single page, you need to have ways to direct the user from place to place. Those typically can be done with links, but in order for those links to work, you need to have routes planned on the back end. In the case of Django, it works like this.

Upon creating a new project in Django, you will find a ‘urls.py’ file, and if you open it you will see something like this:

urlpatterns = [
path('admin/', admin.site.urls),
]

What lines like this do is build out a template for a url. If that url begins with ‘admin’, Django will try to find the corresponding view and do whatever action is defined in that view.

This is also where we can add our own urls, and we can link them to other apps that have been created in the project. In the following examples, I’mg going to reference code from a social media site I built. So in that same ‘urls.py’ file I added:

urlpatterns = [ path('admin/', admin.site.urls), path('', include('landing.urls')), path('social/', include('social.urls')),
]

The empty quotes in the second url line means that it will be the root path for the application, or the default spot the user will end up at upon using the application.

Both of the new lines mean that Django will look in the landing and social app folders and look at the urls. Those files aren’t automatically created to they need to manually be done in whatever code editor you are using. Let’s take a look over to my ‘landing/urls.py’ file and see what I setup on the other side.

from django.urls import path
from landing.views import Index
urlpatterns = [
path('', Index.as_view(), name='index'),
]

As I want to render whatever is in the Index view. I import it at the top of the file. When defining the url, I use the ‘as_view()’ method on the index view, and then I give that url the name of ‘index’. Naming urls make it easier to reference them in template throughout the project.

Because the url is rendering the Index as a view, I need to tell that view what to render in order to complete the url. In my Index view I tell it:

return render(request, 'landing/index.html')

Now I build a template in the landing app called ‘index.html’ and whatever code is in there will be displayed to the user!

To reference urls in other templates is very simple, and it has to do with naming urls as shown above. Django has a tag syntax which causes some of logic like a loop to run. Using ‘{% %}’ will run what ever code is placed in side those tags, so if we want to build our url into a link tag, we can simply:

<a href="{% url 'index' %}" class="btn btn-light mr-2">Index</a>

The class part of that tag is just some styling, don’t get too caught up on it.

This will render a link to the user that when clicked, will navigate the user to the index url that we have defined!

Understanding urls is essential to building out any project no matter what the framework. It greatly improves the user experience and more importantly, helps to show off the great code you’ve worked so hard on!

Software Engineer in NYC with expertise in Ruby on Rails, JavaScript, and React