Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Django

Similar presentations


Presentation on theme: "Introduction to Django"— Presentation transcript:

1 Introduction to Django
Lecture 20 Introduction to Django

2 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 or sudo service httpd start If the server goes down during use, and after every time you edit the configuration files, use sudo systemctl restart httpd.service 2

3 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: 3

4 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. 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. 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) 4

5 Install Postgres and psycopg2
For Windows and OSX, follow the directions on the Django tutorial site. For Fedora: sudo dnf install postgresql sudo dnf install postgresql-server sudo postgresql-setup –initdb sudo dnf install postgresql-devel? sudo postgresql-setup --initdb psycopg2 is an adapter that connects Python to Postgres: sudo dnf install python3-psycopg2 start postgres server in Fedora: sudo systemctl start postgresql 5

6 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 top match the settings in 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 [username] owner [username] \q quits portgres \h gets you a postgres help menu This link has information on Postgres configuration with Fedora: 6

7 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

8 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

9 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

10 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

11 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 will have to redeploy the project to Apache on Digital Ocean when you are finished. Do not assume that will go smoothly. Use Django 1.9 on your home machine, since that is the version Fedora 25 supports. You can try to install a later version of Django on Fedora, but if you do, start very early 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 are worried about the security warning, you can try to get Django 1.11 working on Fedora, but I have not tried this yet. If you write the application using 1.11 and try to deploy in 1.9, you will certainly run into undocumented problems. 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. I can't vouch for any of them. Make sure any instructions you try are for Python 3 and Django 1.9. 11

12 Django The online instructions are sometimes not quite right for Fedora. One very important example: when the instructions say to type python, type python3. Install Django for Python 3: sudo dnf install python3-django I did not use a virtualenv, but you may follow the instructions to do so 12

13 Start a Django Project Stop! Read this before you create the project! The official instructions for the Django demo project are at 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. 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 13

14 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. command to create project in Fedora: python3-django-admin startproject mysite command to run the Django development server: python3 manage.py runserver 14

15 Apache and mod_wsgi 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, but not 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 15

16 Apache and mod_wsgi You will need to migrate the database again on DO. See the Django documentation 16

17 Django 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. 17

18 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. 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/ 18

19 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/ 19


Download ppt "Introduction to Django"

Similar presentations


Ads by Google