Tuesday, July 9, 2019

My first Django app

 
localhost:8000/polls/
localhost:8000/polls/about

DIRECTORY STRUCTURE

D:\mywebsite\Djwebsite
Djwabsite has following files and folders in it:
       folder: Djwebsite ( nested Djwebsite)
       folder: polls (this is the name of the 'app' created in the project   'Djwebsite'

     file: manage.py
   
The 'polls' folder has following files and folders:
       files: apps.py
                views.py
                urls.py
                models.py
                admin.py
                tests.py

---------------------------------------------------------------------------------------
 'apps.py' file:

from django.apps import AppConfig


class PollsConfig(AppConfig):
    name = 'polls'
-----------------------------------------------------------------
'D:\mywebsite\Djwebsite\polls\urls.py' file:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('about', views.about, name='about')
]
--------------------------------------------------------------------
'views.py' file:

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
site = '''<!DOCTYPE html>
<html>
<head>
<title>butube</title>
</head>
<body>
<font color=green size=10><h1=lightgreen>butube</h1></font><hr color=lightgrey></body></html>
<a href="/polls/about">about</a>
'''
def index(request):
    return HttpResponse(site)
   
def about(request):
    title = 'butube'
    author = 'Aksingh'
    html = '''<!DOCTYPE html>
    <html>
    <head>
      <title>''' + title + '''</title>
    </head>
    <body>
        <a href="/polls">home</a>
        <h1>Welcome to ''' + title + '''</h1>
        <p>This Website was developed by ''' + author + '''.</p>
       
    </body>
    </html>'''
    return HttpResponse(html)

---------------------------------------------------------------------------------
'D:\mywebsite\Djwebsite\Djwebsite\urls.py' file:

"""Djwebsite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('polls/', include('polls.urls')),

    path('admin/', admin.site.urls),
]

-----------------------------------------------------------------------------------


No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows