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
- 1. Introduction
- 2. Installation and setup
- 3. How things work
- 4. The first Django app
- 5. Your first view
- 6. Your first template
- 7. Testing our site
- 8. Extending templates
- 9. Your first model
- 10. The admin interface
- 11. Showing contents from the model
- 12. Another app (category)
- 13. Connecting two models
- 14. Show list of categories
- 15. Category detail page