the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

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, 454 views


Leave a Reply

Your email address will not be published. Required fields are marked *