Create the "nearbyshops" View
Nearly everything for the backend of our application is complete. In this video we’ll concentrate on the frontend in order to give our user a way to see the shops near their location.
What we’ll do in this video is create a Django view in our nearbyshops app and then route users to that view through the main urls.py.
It might help to revisit what happens with an incoming request, which we covered in video #6.
Downloadable Sample Project: This course includes a downloadable sample project that demonstrates the final state of the Django app you’ll build throughout the course. You can get the source code for it here on the
00:00 Welcome to video number ten in our series on making a location-based web app with Django and GeoDjango. Nearly everything for the backend of our application is complete. In this video, we’ll concentrate on the frontend, in order to give our user a way to see the shops near their location.
It might help to revisit what happens with an incoming request, which we covered in video number six. Our Django project is called
shops. It was created by the
startproject script, so it includes the starter files
It acts as the main gate to our website. Our Django app is
nearbyshops, a component of the
nearbyshops was created as an app by the
startapp script. This created a folder of the same name, which included starter files for building responses to incoming requests.
You’ll do the following three things to make all of this work: You’ll create a view in
views.py is already there, we just have to fill in some missing code. You’ll create an HTML template.
The template is not there, so we’re going to have to build that from scratch. And then finally, we’ll add a route to the view in
urls.py. So let’s get started with that
views.py file and create a view.
views.py is located in your app folder—in this case, the
Let’s take a look at what’s inside
views.py. Since our app is new, the file is empty except for a small amount of starter code. But typically in a Django app, you’d find functions and classes here that would correspond to various routes and responses.
02:09 You’ve heard me say before that Django is a batteries included framework. One of the batteries that Django provides is generic views. Some webpage views are so common, Django provides classes for them.
The first entry,
'BACKEND', tells us what template language Django will use. In this case, the default is the Django template language, or DTL. Newer versions of Django can use others such as Jinja.
The second setting I want to explain is the
'APP_DIRS' setting that’s set to Boolean
True. With this setting
True, Django will expect to find templates inside of the app folder—in our case,
nearbyshops/. Not only that, it will expect to find the templates in a specific directory named
04:53 Now, our path is correct. Templates are just HTML files that include special template syntax mixed in. This syntax is often referred to as handlebar or mustache syntax, and its job is to format dynamic data into your page.
Your last task in this video is to add your routes. This is handled by the project side in
urls.py. Let’s create it now. For
urls.py to reference anything in
views.py it needs access, so import
views as a module by way of
nearbyshops, like this.
Finally, add your route to
urlpatterns by using the
path() method. The arguments will be your site route as a string, followed by the view that should respond. Our path is the root, so it’s an empty string, and the response will be our
06:20 And here we go! Here’s the closest shops nearest to our pretend user. Great job. Thanks for sticking with me so far. If you can handle just one more video, I’ll show you how to make this page prettier with Leaflet.
Become a Member to join the conversation.