Django studies a love poem

   Django is one of the most representative frameworks for Web development in Python. This article will give y...

   Django is one of the most representative frameworks for Web development in Python. This article will give you a brief introduction to Django development.
   first make sure that the django module is installed on your linux system. Open Python 3 and use the following command to find the directory where django is located:

>>> import django >>> print(django.__path__)

The author shows that:

['/usr/lib/python3/dist-packages/django']

Next, start django development! First, switch to the django directory and create a new poem project

django-admin.py startproject poem

Enter tree poem to view the tree structure of the poem project


Tree structure of poem project

stay urls.py In the urlpatterns list, add:

url(r'^$', 'poem.views.output'),

In the / poem/poem folder, create views.py File, input code:

from django.http import HttpResponse def output(request): title = "<h1>When You Are Old</h1>" author = "<h2>William Butler Yeats</h2>" content = """ When you are old and grey and full of sleep,<br/> And nodding by the fire, take down this book,<br/> And slowly read, and dream of the soft look<br/> Your eyes had once, and of their shadows deep;<br/> How many loved your moments of glad grace,<br/> And loved your beauty with love false or true,<br/> But one man loved the pilgrim soul in you,<br/> And loved the sorrows of your changing face;<br/> And bending down beside the glowing bars,<br/> Murmur, a little sadly, how love fled<br/> And paced upon the mountains overhead<br/> And hid his face amid a crowd of stars.<br/> """ return HttpResponse([title, author, content])

Enter the poem folder and enter the command:

python3 manage.py runserver 8000

   open browser, type localhost:8000 The page is as follows:

Web display
In this way, we have completed the development of a simple django project. Looking forward to sharing in the next period~~

This is the end of this sharing. We welcome your criticism and exchange~~

1 July 2020, 12:05 | Views: 8928

Add new comment

For adding a comment, please log in
or create account

0 comments