Monday, March 11, 2019

Django: TechReview Project

The Techreview project is a simple Django project that lists tech products and hosts reviews of them. Anyone can see view the site, but only registered users can add products and reviews. This tutorial will be divided into nine parts:
  1. Installations and setup
  2. Creating the first view
  3. Add a model and migrating it. Admin site.
  4. Create a view that displays data
  5. View and details view
  6. Tests
  7. Forms and form tests
  8. Authentication
  9. Extras

Installation and Setup


For Django to work we need to set up the environment. We are going to need three major things:

  • Python 3
  • PostgreSQL database server and pgAdmin
  • Django

I am going to mostly talk about setting things up in Windows. In class we are using Ubuntu Linux on a Virtual Machine, where most of the components are already set up. I will, however, occasionally compare the Linux and Windows commands.

We will also be using a fourth component VSCode, though if you prefer you can use IDLE or another IDE that runs Python.

VsCode is free and runs on any platform. It can be found at https://code.visualstudio.com/

On Windows, if you don't have python you can go to https://www.python.org/ and download the language. Make sure when you install that you check the little box that says add to path. That way python will be available at the command line without having to go look for the directory.

PostgreSQL can be found at https://www.postgresql.org/. A download and installation of the database will also install pgadmin, the graphical interface for PostgreSQL. Make sure you remember the password you give it when you install, otherwise you could end up locked out.

Once python and PostgeSQL are installed, you can create a python virtual machine and install django.

Here are the Linux commands in Ubuntu

python3 -m venv venv

source venv/bin/activate

pip install django

django-admin startproject techreviewproj

cd techreview

python3 manage.py startapp techapp

In Windows use the cmd.exe or powershell. (Sometimes powershell has issues with running python commands.) The only differences are you probably do not need to say "python3"--"python" is sufficient. Also to start the virtual machine key in:

venv\scripts\activate

Opening the Project in VSCode

*******************************************************************

View this for how to set up the Github repository and then open it in Visual Studio Code

********************************************************************

Open VSCode and From "File" menu select "Open Folder." Open the outer project, in our case "techreviews"

You should have two subfolders on the same level: Techreveiwproj and techapp. The Techreviewproj contains the project level files. The two most important for our uses are "settings.py" and "urls.py." We will look at both momentarily.The techapp folder contains most of the files we will use for actually developing out application: admin.py, models.py, tests.py and views.py. We will add additional folders and apps to the techapp directory as we build the application.

Register the App

Open settings.py in techreviewproj. You may need to install the python extension for VSCode. If prompted, do so. Install it and then reload.

There are two parts of the settings.py file that concern us right now. The first is the installed_apps section. Add the name of current app (techapp) to the list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'techapp',
]

This registers the application with the project. A project can contain several apps. Ours will only contain this one. If you fail to add the app name to the list, the program will fail to locate your files and web pages.

Setup the Database

(This post shows an alternate way to do the database setup without revealing sensitive passwords and server info.)

The second section we need to work with is the database. Django defaults to a local installation of SQLite. But we want to use something more substantial. We are going to use a PostgreSQL database called "techprojectdb." We need to configure the Engine, the database, the user and password and the host. You may have to add the port if it is not the default port.

DATABASES = {
   'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'techprojectdb',
        'USER' : 'postgres',
        'PASSWORD': 'P@ssw0rd1',
        'HOST' :'localhost',
        'PORT' :'',
    }
}

The techprojectdb database does not exist. We need to create it in PostgreSQL. Assuming that you have installed it and pgAdmin4, open pgAdmin now. Connect to the server and then right click on "databases." Select CREATE Database:

Key in the database name. It needs to match the name of the database in settings.py exactly including in case.

Save it and return to Vscode.

Make sure that you have saved the changes to the settings.py file. From the menu choose terminal. In Ubuntu it will open a bash shell terminal. In Windows it will open Powershell. Powershell may not work with python so you might want to change the Terminal to cmd.

From the techreviewproj folder key in the following command:

techreviewproj> python manage.py migrate

In Linux you will need "python3" instead of just "python." The migrate command will write all the system and authorization tables to the database techprojectdb which we just created. If you look in pgadmin under our database in schemas/public/tables, you will see these new tables:

Redirect the Project URLS

We have one more setup task to do before we start working on our actual app. We want to redirect the urls.py from the project to a urls.py in the app. This is a good idea in general and especially if you have multiple apps. Open the urls.py in techreviewproject. Change it to look like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('techapp/', include('techapp.urls'))
]

Note that we added "include" to the import from django.urls, and we added a path statement that redirects the program to look for urls in the techapp folder. Now in the techapp folder, add a new file called urls.py

Now we are all set up and configured and ready to begin developing the app.

Next Section

No comments:

Post a Comment