Python/Django simple application to print














































Python/Django simple application to print



We need to create a Django project and a Django app:


django-admin startproject my_first_django_project
cd my_first_django_project
python manage.py startapp hello

We now have a project named my_first_django_project and an app named  hello

hello/views.py


import textwrap
 
from django.http import HttpResponse
from django.views.generic.base import View
 
 
class HomePageView(View):
 
    def dispatch(request, *args, **kwargs):
        response_text = textwrap.dedent('''
            <html>
            <head>
                <title>Greetings to the world</title>
            </head>
            <body>
                <h1>Greetings to the world</h1>
                <p>Hello, world!</p>
            </body>
            </html>
        ''')
        return HttpResponse(response_text)

hello/urls.py


from django.conf.urls import url
 
from hello.views import HomePageView
 
urlpatterns =[
    url(r'^$', HomePageView.as_view(), name='home'),
]

my_first_django_project/urls.py


from django.conf.urls import include, url
 
urlpatterns = [
    url(r'', include('hello.urls')),
]

my_first_django_project/settings.py


INSTALLED_APPS = [
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]
 
MIDDLEWARE_CLASSES = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]



Our Django site is ready. Start it like this:


python manage.py runserver

And then visit http://localhost:8000/ in your browser.


Comments