Download presentation
Presentation is loading. Please wait.
1
Lecture 17 Django
2
Django Framework for web programming using Python
Automates many common tasks Named after jazz musician Django Reinhardt Pinterest, Instagram, and many other complex web apps use Django. Originally created for the web site of a newspaper, the Lawrence Journal-World. Several recent Senior Design projects have used Django Django Reinhardt
3
Django Projects and Apps
In Django terminology, a "project" is a web environment-the configuration and applications on a particular web site. There may be more than one application in a project. 3
4
Object-Relational Mapping
Web applications typically use OOP Data is typically stored in relational databases If a data value in an object is non-scalar (say, it is a list or array), or if the value is of some type that itself has non-scalar fields, the relationship between the class and the database tables is complex. Consider a Student object with a list of courses, each of which has one or more instructors who may also teach other courses. We will need several cross- reference tables in the DB. OO Inheritance makes database design even more difficult Like many other web programming frameworks, Django provides automatic Object- Relational Mapping
5
Automatic CRUD Most software applications involve creating and processing data CRUD = Create, Read, Update, Delete Django was developed for a newspaper website, in which some users provided data and others consumed it Django includes an automatically generated administrative user interface for CRUD functions
6
Model-View-Template Most web frameworks use Model-View-Controller pattern Django is not really very different, but it uses different nomenclature Data and interaction with the DB = Model Application logic to manipulate data = View (this is an unusual use of the word view!) Generic html pages that receive and display specific data at runtime = Template
7
Django Views and Templates
Django’s model for rendering web documents is described as a Model-View- Template system. The difference between this and Model-View-Controller is mostly just a difference in terminology. As with MVC, the “Model” consists of the data you are handling in the application. For this part of your code, the fact that you are presenting the application as a web app using Django is not important. This part would be similar for a desktop or mobile application. The model should be completely independent of the UI. The document the user receives is generated by the view, which is a Python code file, *not* an html or xml file. Views, however, typically use templates, which contain html with elements that handle data supplied by the view using a templating language. The basic principle is very similar to Java servlets. 7
8
Django Views and Templates
We must map url patterns to view files so that the application can find the right views. This configuration is done by specifying a) regexes to apply to urls and b) paths to view files, in a file named urls.py. from django.conf.urls import url from django.contrib import admin from demo import views urlpatterns = [ url(r'^admin/', admin.site.urls), # admin has its own url mappings url(r'^$', views.index, name='index'), ] This urls.py works at the project level. You also need separate urls.py files for each application, which you would reference here using the include statement. 8
9
Django Views and Templates
It is possible to generate a view entirely using Python code. You may have done broadly similar coding using client-side JavaScript. views.py: from django.shortcuts import render from django.http import HttpResponse # Create your views here. def index(request): out = '<h1>Celsius to Fahrenheit Conversions</h1>' for i in range (-40, 101): out += str(i) + ' deg. C' + ' = ' + '{:.1f}'.format(i * 9/5 + 32) + ' deg. F<br />' return HttpResponse(out) view this locally at :8000 9
10
Django Views and Templates
Views created entirely with Python are hard to debug and edit. It is more productive to handle the html and Python logic separately. Add a line to INSTALLED_APPS in your settings.py file that informs the project about your application. This will be critical for several tasks, including getting it to search for your html templates. My app example is called demo. This code is in djangodemo/mysite/mysite/settings.py: INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'demo.apps.DemoConfig', ] Put the templates inside the application directory, but in a subdirectory called templates/appname (for example, mysite/demo/templates/demo) 10
11
Django Views and Templates
Templates contain html with additional elements that use a language very much like JSP element syntax for relating application data to html. This document contains reference information for the template tag language: The context is the data applied to the template by the view. In Django, the context is a Python dictionary consisting of variable names and their values. 11
12
djangodemo/mysite/mysite/urls.py:
from django.conf.urls import url from django.contrib import admin from demo import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^conversion', views.conversion, name='conversion'), ] djangodemo/mysite/demo/views.py: from django.shortcuts import render from django.http import HttpResponse def conversion(request): conversions = [] for i in range (-40, 101): curr_tuple = (i, i * 9/5 + 32) conversions.append (curr_tuple) # list of tuples context = { 'conversions': conversions, } return render(request, 'demo/conversion.html', context) 12
13
djangodemo/mysite/demo/templates/demo/conversion.html:
<h1>Celsius to Fahrenheit Conversions</h1> {% if conversions %} {% for pair in conversions %} {{ pair.0 | stringformat:".2f" }} Deg C = {{ pair.1 | stringformat:".2f"}} Deg F<br /> {% endfor %} {% else %} <p>No Data!</p> {% endif %} view this locally at :8000/conversion 13
14
Models It’s not a good practice to do calculations in a view. Its purpose is to provide I/O, and it should not be so tightly coupled to data manipulation. If we were doing something more complicated, we would want the processing thoroughly separated from the GUI, so that we could change either one without touching the other. Revised djangodemo/mysite/demo/models.py : from django.db import models class Converter(models.Model): def convert(self, c): return c * 9/5 + 32 14
15
Models Revised djangodemo/mysite/demo/views.py:
from django.shortcuts import render from django.http import HttpResponse from .models import Converter def conversion(request): c = Converter() conversions = [] for i in range (-40, 101): curr_tuple = (i, c.convert(i)) conversions.append (curr_tuple) # list of tuples context = { 'conversions': conversions, } return render(request, 'demo/conversion.html', context) 15
16
Forms So far, this example has shown output, but not taken any input. For input, we can use html forms. This architecture is not very different from servlets, but is uses different vocabulary: Take user input using a form in a template (not called a “view”). Map the form action to a view function (not called a “controller”) in urls.py View processes data using classes or DB access from the model View creates a context View renders a different template, sending it the context Context presents the new data Note the form action in input.html-this is the map to the next view 16
17
Forms input.html: <h1>Celsius to Fahrenheit Conversion</h1> <form action="result" method="post"> {% csrf_token %} # <label>Input temperature in Celsius: <input id = "cels_input" name = "cels_input"/></label><br /> <input type="submit" value="Convert" /> </form> 17
18
Forms result.html: <h1>Celsius to Fahrenheit Conversion Results</h1> {% if result %} {{ result.0 | stringformat:".2f" }} Deg C = {{ result.1 | stringformat:".2f"}} Deg F<br /> {% else %} <p>No Data!</p> {% endif %} 18
19
Forms views.py: from django.shortcuts import render
from django.http import HttpResponse from .models import Converter def input(request): context = {} return render(request, 'demo/input.html', context) def result(request): c = Converter() cels = int(request.POST['cels_input']) result = (cels, c.convert(cels)) context = { 'result': result, } return render(request, 'demo/result.html', context) 19
20
Forms models.py: from django.db import models
class Converter(models.Model): def convert(self, c): return c * 9/5 + 32 20
21
Forms djangodemo/mysite/demo/urls.py from django.conf.urls import url
from . import views urlpatterns = [ url(r'^input', views.input, name='input'), url(r'^result', views.result, name='result'), ] djangodemo/mysite/mysite/urls.py from django.conf.urls import url, include from django.contrib import admin from demo import views url(r'^admin/', admin.site.urls), url(r'^demo/', include('demo.urls')), #refer requests for urls with "demo" to the app's urls.py 21
22
Debugging Hints The command line you use to run the development server often shows helpful error messages Django and the development server support hot-swap; you usually do not need to restart the server when you change the code. Hot swap works much better on the development server than in most servers that claim to support it. However, it doesn’t work if you change url mappings (see below). Also, note that your browser may cache responses, so you may need to reload several times to see changes. 22
23
Install Apache Web Server
Apache Web Server is the most widely used web server Open source Usually running in Linux or UNIX, but there are versions for Windows and OSX Install it on your Digital Ocean VM. You do not need it on your local machine, since Django includes a lightweight server for development. To install the basic functionality of Apache in Fedora: sudo dnf install httpd You can also look up how to install additional functionality to support servlets, etc. to start up: sudo systemctl start httpd.service If the server goes down during use, and after every time you edit the configuration files, use sudo systemctl restart httpd.service 23
24
Install Apache Web Server
Verify that the install and startup worked by entering your VM's ip address in a browser on your client machine. You should see this test page: 24
25
Install Apache Web Server
Create a home page in Apache: Navigate to /var/www/html. This will require sudo. This directory is the root of your website; create any additional directory structure you need within html. Use a separate directory *inside html* for css. 25
26
Install Apache Web Server
Recall that web servers automatically serve index.html or index.htm if no file name is provided. The file owner for index.html should be root. Use sudo if necessary to accomplish this. This home page does not need to contain anything except a simple message. The point is simply to make sure Apache is working correctly. Fedora and many other Linux distros use an additional layer of security called SELinux. Use this command to tell SELinux to allow the world to see your index.html: sudo chcon -v --type=httpd_sys_content_t index.html Note that the URL for your home page will *not* include the /var/www/html. The server is set up to "map" urls into this directory. If your ip address is , the URL of your index.html is just See Digital Ocean's documentation if you want to get a domain name and/or use TLS (SSL) (both very cheap) 26
27
Install Postgres and psycopg2
For Windows and OSX, follow the directions at For Fedora: sudo dnf install postgresql sudo dnf install postgresql-server sudo dnf install postgresql-devel (may be needed) sudo postgresql-setup --initdb psycopg2 is an adapter that connects Python to Postgres. I tried sudo dnf install python3-psycopg2 but this did not seem to connect psycopg2 to Django. sudo pip install psycopg2 worked better. start postgres server in Fedora: sudo systemctl start postgresql If the start operation fails, follow the instructions to get more info. You may need to sudo dnf install postgresql-upgrade then sudo postgresql-setup --upgrade 27
28
Use Postgres, not sqlite
The Django sample application instructions assume you will use sqlite, but when you deploy the app to Digital Ocean, you will need to use Postgres. I suggest you create the sample app using Postgres in the first place. When you deploy the app to DO, you will need to create the user and set the password to match the settings in the file settings.py. You will also need to set db permissions. There are instructions online at: Start Postgres as user postgres the first time like this: sudo -u postgres psql change postgres' password: alter user postgres password [new password here, in single quotes]; create username and password for the user: create user [username here] createdb password [password here, in single quotes]; create database [database name] owner [username]; \q quits postgres \h gets you a postgres help menu This link has information on Postgres configuration with Fedora. 28
29
Django Assuming you use a Windows or OSX computer for your homework, you may approach this project in either of two ways: 1) Develop the project in your native operating system 2) Use VirtualBox to run Fedora on your own machine Either way, use the Django development server on your own computer. You may redeploy the project to Apache on Digital Ocean when you are finished. Do not assume that will go smoothly. Most students last year found that deployment was the hardest part of the project. Use Django 1.11 on your home machine, since that is the version you will get with pip in Fedora If you write the application using Django 2.0 and try to deploy in 1.11, you will certainly run into undocumented problems. Most of the instructions that follow concern Fedora. The Django documentation contains instructions for installing Django and starting a project in Windows at If you use OSX, go slow and think about ways the instructions may not apply to OSX. Installation using Terminal is probably reasonably similar to the Fedora instructions here. It is undoubtedly also possible to do most of it using the GUI. There are many sets of instructions online, but I can't vouch for any of them. Make sure any instructions you try are for Python 3 and Django 1.11. 29
30
Installing Django in Fedora
Make sure your python3 is up to date: sudo dnf update python3 Install Django for Python 3: sudo python3 -m pip install django I did not use a virtualenv, but you may follow the instructions to do so 30
31
Start a Django Project Stop! Read this before you create the project! The official instructions for the Django demo project are at The online instructions sometimes need some alteration for Fedora. One very important example: when the instructions say to type python, type python3. Read the instructions in parallel to these slides, and go slow. Always check the slides before you follow an instruction from the documentation. Also, always read ahead to the end of a page in the instructions before you do anything. If something does not work, figure it out and fix it before you move on to the next step. On Digital Ocean or any other machine where you plan to run a webserver, don't put your project inside /var/www. That is where Apache puts web documents by default. Since this part of the file system can be accessed directly by the server, it would be easier for unauthorized users to read your Python code if you put it here. Put it someplace else. The Django documentation suggests /home/mycode 31
32
Start a Django Project If you are developing in Windows or OSX, you will not need to start a project in Fedora, since you will instead be uploading a project you created in your native OS. If you use Fedora for development: command to create project in Fedora: django-admin startproject mysite command to run the Django development server: python3 manage.py runserver 32
33
Apache and mod_wsgi You can get full credit for this assignment by deploying the project on Digital Ocean using the Django development server. However, it would be much better to use Apache, which is more scaleable and more secure. When you are are ready to deploy to D.O., upload your code using sftp. If you are ambitious about this, you can find instructions to use git instead. mod_wsgi is an extension to Apache that supports Python. You will need to install it on the Digital Ocean VM if you use Apache. You will not need to install it on your homework computer sudo dnf install python3-mod_wsgi See the installation instructions at and the deployment instructions at Ignore the recommendation to use an additional server for static and media files. You may also need to add the ip address to Allowed_Hosts in settings.py You can find httpd.conf this way: sudo find / -name httpd.conf don't edit the copy in tmpfiles; there is probably one inside /etc When you edit httpd.conf, the changes take effect only after sudo systemctl restart httpd 33
34
Apache and mod_wsgi You will need to migrate the database again on DO. See the Django documentation 34
35
SE Linux SELinux is an additional layer of security functionality included by many Linux distros, including Fedora. allow access to files and directories like this: sudo chcon -v --type=httpd_sys_content_t wsgi.py do this for each of the directories starting with mysite and each of the .py files. 35
36
Serving static files In Django terminology, css, background images, media files, etc, are static files This link contains instructions for serving them in your application with Apache. Follow the instructions for using the same server you use for the rest of the app. You can write your own css if you'd like to, but you can also just copy the ones used in the sample app. They are probably in /usr/lib/python3.5/site-packages/django/contrib/admin/static/admin/css/ 36
37
What the heck is wrong? Read error logs early and often!
Find Apache error logs: sudo find / -name error_log read the log: sudo cat /var/log/httpd/error_log (is that is the right path) read just the last 50 lines of the error log: sudo cat /var/log/httpd/error_log | tail -n 50 Postgres error logs will be in files in a directory called pg_log. They will probably be in /var/lib/pgsql/data/pg_log/ 37
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.