Sunday, June 24, 2018

Update on Current Projects.

PostgreSQL

I am still working on PostgreSQL. Mostly I have been working on creating and populating databases that I can use with my classes next year. I also intend to write a book on SQL using PostgreSQL as the Database Management System

I would like to have at least 3 fully populated databases in place. Currently I am adapting CommunityAssist. I also have added a Certificate Database that tracks students in an on-line school--the courses they take and ther certificates they earn. I may also do a Dentist shop.

I also need to explore the deeper features of PostgreSQL including its use of schema, building functions and triggers and security and roles.

Django

I am also exploring Django as a possible platform to replace the .Net MVC that I have been doing in ITC 172. It has the advantage of providing further programming in Python, something for students to build on after their beginning programming class.

So far i opened a Django project in Visual Studio and was instantly lost. I couldn't make heads or tails of it. So I started following an on-line book available at https://djangobook.com/. It guided me through installing an building the framework from scratch. I found that immensely helpful. So far I have installed the Django, set up the directories, started the virtual environment, and created a few simple pages by adding to views.py and urls.py and by creating a template.

A simple Django page

I think I know enough now to use the Visual Studio version if I wished. But I think I will stick with the manual setup for awhile.

Here is the code for the view.py. There is some extra code for hello world and another time plus:

from django.http import Http404, HttpResponse
import datetime
from django.template.loader import get_template

def hello(request):
    return HttpResponse("Hello World")

def current_datetime(request):
    now=datetime.datetime.now()
    t=get_template('current_datetime.html')
    html=t.render({'current_date':now})
    return HttpResponse(html)

def hours_ahead(request, offset):
    try:
        offset=int(offset)
    except ValueError:
        raise Http404()
    dt=datetime.datetime.now() + datetime.timedelta(hours=offset)
    html="</html><head><title>Time offset</title><head><body><h2>In %s hour(s), it will be %s.</h2> </body></html>" % (offset, dt)
    return HttpResponse(html)

Here is the code for the urls:

"""firstdjangoproject URL Configuration

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello/$', hello),
    url(r'^time/$',current_datetime),
    url(r'^time/plus/(\d{1,2})/$', hours_ahead),
]

And here is the code for the template:

<!--firstdjangoproject\firstdjangoproject\templates\current_datetime.html-->

<html>
    <head>
        <title>Current date and time</title>
        <style>
            h1 {color:navy};
            p {font-family:serif}
        </style>

    </head>
    <body>
        <h1>Current Date and time</h1>
        <p>It is now {{current_date}}</p>
    </body>
   
</html>

You also have to update the settings file.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

One of my next steps, after I work more through the samples in the book, is to create a tutorial that goes through the process of creating a a django site complete with a connection to a PostgreSQL database.

No comments:

Post a Comment