...

Open source softwares - Django

Back to Course

Lesson Description


Lession - #991 Django-Templates


Templates

In the Django Intro page, we discovered that the result should to be in HTML, and it should to be made in a template, so we should do that.

Make a templates folder inside the individuals folder, and make a HTML document named myfirst.html.

The document structure should to be something like this:
myworld
    manage.py
    myworld/
    members/
        templates/
            myfirst.html


Open the HTML file and insert the following:

members/templates/myfirst.html
<!DOCTYPE html>
<html>
<body>

<h1>Hello World!</h1>
<p>Welcome to my first Django project!</p>

</body>
</html>


Modify the View

Open the views.py file and replace the index view with this:
members/views.py
from django.http import HttpResponse
from django.template import loader

def index(request>
: template = loader.get_template('myfirst.html'>
return HttpResponse(template.render(>
>


Change Settings

To have the option to work with more convoluted stuff than "Hello World!", We need to let Django know that a new app is made.

This is finished in the settings.py document in the myworld folder.

Look into the INSTALLED_APPS[] list and add the members app like this:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'members.apps.MembersConfig'
]


Then run this command:
py manage.py migrate