3. Creating a new hello app
1. Let’s create some code.
Navigate to the directory which contains manage.py (cd my_django_app) and start the webserver
$ python manage.py runserver
2. Go to VSC – we previously gave the app a name called hello, so the .py files we want are in the hello directory.
3. To start making our simple app we configure the database, by going to the models.py file, and adding this class:
from django.db import models class Hello(models.Model): text = models.CharField(max_length=200)
What the models file does is describes potential database tables that we are going to create.
class maps to a table, so there will be a table in the database called hello
The class variables (eg text) are the fields in the database
So underlying our whole application will be this database description.
4. When users map requests, we have to know how to map requests to code. We do this using the URL routing that Django provides.
We need to go back to the my_django_app directory and open up urls.py
line 17 says from django.urls import path but we want to change this to:
from django.urls import include, path
This imports a new Django function called include which helps to dispatch apps to different paths.
In urlpatters in this file I need to add:
path(”, include(‘hello.urls’)),
Hello is the name of the app. In the hello directory we need to add a new file called urls.py
I added this code:
from django.urls import path
from . import views
urlpatterns = [
path(”, views.index, name=’index’)
]
I need to go to hello/views.py and add this:
from django.shortcuts import render
from .models import *
def index(request):
response_string = Hello.objects.all()[0]
return render(request, ‘helloworld/index.html’, {‘data’: response_string})
We need to create this template now.
I create a new folder in hello called templates and another one inside templates called helloworld.
Inside here we make a new file called index.html
<html> <head></head> <body> <p>{{ data.text }}</p> </body> </html>
In settings.py we need to add this to installed apps:
‘hello.apps.HelloConfig’, // I think hello.apps.HelloConfig and not helloworld.apps.HelloWorldConfig
5. Now models.py describes a database table that doesn’t yet exist.
We can go back to CMD and back to the my_django_app directory (the one with manage.py), and make a migration.
$ python manage.py makemigrations
0001_initial.py appears in our migrations directory now.
That doesn’t mean it’s in the database. To change the database we need to run the migrations:
$ python manage.py migrate
We need to populate the database as well. So go back to inside the hello directory and create a new file called initial.json. Add this code:
[
{
“model”: “hello.Hello”,
“pk”: 1,
“fields”: {
“text”: “HELLO WORLD!”
}
}
]
Then do this to load the JSON data into the database:
$ python manage.py loaddata hello/initial.json
Run server:
$ python manage.py runserver
This now manages to serve HELLO WORLD, which we added to the database and served from the template.
Wednesday 13 October 2021, 625 views
Next post: 4. Django views Previous post: 2. Creating a new virtual environment
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 29. JavaScript basics
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django
Leave a Reply