Django: Views and URL Mapping

Django: Views and URL Mapping

When I started learning Django, it took me a while to understand the concept of views and URLs mapping especially when you learn from random dudes on YouTube. one time your web app is correctly producing views for each request and the next it's producing errors, and then you ask "what did I do wrong?", I thought this experience would be relatable and I intend to help with this article by explaining the concepts of views and URL mapping and how to use them in your projects.

This article is for beginners and maybe as a refresher for techies who have been working with Django for a while. Yeah, I will be explaining the tutorial with a todo app I created, this will help us understand better and relate every lesson with actually building apps, something I've learned from project-based learning. seat tight, let's delve into it.

Views and URLs

Views are python functions that takes http requests and returns http response which can be displayed to the end user in form of HTML documents. Since the use of Django, most of the time is to build dynamic web apps that need to collect data, store them and also retrieve them to display to the user, the use of view logic comes in handy. when you think of the website, it contains the content on the webpages and a URL to locate these contents. Django follows the same abstraction, You have the Django views that render the content of the webpages and there's a URL associated with each view which is listed in the URL configuration. So views generate the data by interacting with the database, rendering templates and performing other logic to produce responses on the web app.

Using Views and URL Mapping

Let's explore using views and URL mapping in our todo app, suppose you've created a folder that contains a Django project folder. We go into the Django app folder, this folder should contain the following files: admin.py,apps.py, models.pytests.py,urls.py and views.py

The goal is to process HTTP requests and return a response using Django's View and URL. First, You need to go into the views.py file and import the class HttpResponse from the django.http module, then you need to define a view function that takes an HTTP request object as its argument, what will be done is called a function-based view, there are class-based views too which I will be explaining in my later article.

Notice the render() import? render() is a function that combines a given template with a context dictionary and returns an HttpResponse object with the rendered text. Now we defined a function called index, taking an http request object as a parament and returning an http response "Hello World!", You can give the function any name but it's good practice to name functions according to their purpose. we then need to map the view function with a URL, so we then create a 'url.py' file in the app folder, take note that when you start a Django project it comes with a url.py in the project folder but it is good practice to create another 'url.py' file in the app folder. in the newly created 'url.py' file in the app folder, we need to import the path function and also import the view.py file using the app directory but since the 'urls.py' file and the 'views.py' are in the same directory we just add a period before the import keyword. we then create a list sequence called 'urlpatterns', in this list sequence we place the path function in it which requires two arguments, the first is a url pattern string, and the second is the view's relative path and view name.

for now, the URL pattern is set to an empty string, the 'views.index' argument means we are trying to access the index function in the views.py file.

one more thing to finish up. Take note that when a user makes a request using a url, the request is handled by the 'url.py' in the project folder created by default when you start a django project. But we've created another 'url.py' in the app folder which contains the code for the URL mapping, so we Django looks at the 'url.py' in the project folder we need to redirect it to our 'url.py' file in the app folder. To do this, we go into the 'url.py' file in the project folder and import the path() and include() function.

we create a new path in the 'urlpatterns' list, inside the path function we reference the 'url.py' file in the app folder as the view argument, with this include() function the 'url.py' file in the project folder can inherit from the 'url.py' file in the app folder.

for this example, the name of my app folder is called 'todoapp' since I'm creating a todoapp app, this folder is inside another folder called 'todo', hence to access the url file we need to provide the path 'todo/todoapp.url'.

And that's it, we are done! You can run the server using 'python manage.py runserver' in the development terminal.

In this article, we've learned about views, URLs and how we can map URLs to views. we also learned how we can redirect Django from the 'url.py' file in the project to the 'url.py' file in the app folder.