Testing our site - 30 days of Django

Testing our site - 30 days of Django

/ #Django


Let's run the development server and test our website in the browser.

The urls.py file

The project is almost ready for testing, we just need to add the front page view to the list of available urls.

It's best practice to have a separate urls.py file for each of the Django apps. But in this part I will only use the main urls.py file to keep things simple, and then we'll come back to fix it later.

Open up "toodoo/urls.py". First we need to import the front page view. So above the "urlpatterns" list, add this:

from task.views import frontpage

Next, we need to add it to the list of urls. So inside the urlpatterns list, add this (above the admin url):

path('', frontpage, name='frontpage'),

What happens here is that we use a function from Django called "path" to set up the url.

The first parameter "''" means that there is no url. So when we go to the domain or website address, this url will be triggered.
The second parameter "frontpage" is the view we want to call when we visit the url.
And the third parameter "name='frontpage'" is just a global name for this. This makes it easy to reference it in our project.

Nice! Now we can continue to the testing :-D

Running the webserver

For development purposes, Django comes with a built in webserver we can use while testing. To run this, you need to go to the command line and make sure you're in the same folder as the "manage.py" file.

When you're there, run this command.

$ python3 manage.py runserver

It will look something like this:

System check identified 12 issues (0 silenced).
April 27, 2022 - 04:27:06
Django version 4.0.3, using settings 'toodoo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now you should be able to go to 127.0.0.1:8000 in your browser and see the front page we have created!

The project isn't very impressive yet, but hang in there!

Table of contents

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.