leo-y
2/2/2018 - 9:26 PM

django quickstart

17-437/17-637 1/25/2017

Django Quickstart

For this class, many of the homeworks will concern creating and editing Django applications. Django ( ) is a popular web framework written in Python.

https://www.djangoproject.com/

Installation

1. Install Python 2.7 or Python 3 if you haven’t done so already.

2. Install Django 1.11.

• We recommend using pip to install Django

If using Python 3, you may need to use the commands python3 and/or pip3

(but there are other options – see

doc

):

i. Install pip if you haven’t already done so (see

doc

):

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

python get-pip.py

pip install –U pip

(on MAC: sudo python get-pip.py)

(on Windows: python –m pip install –U pip)

ii. Then install Django 1.11:

pip install django==1.11

(on MAC: sudo pip install django==1.11)

Note: double ==

3. Outside of your class repository, clone one of the class examples and try to run it:

git clone https://github.com/CMU-Web-Application-Development/django-intro.git

cd django-intro

python manage.py migrate

python manage.py runserver

4. Visit http://localhost:8000 and verify that the application is working.

Creating a Django Project

Create a new Django project for Homework #3:

1. Connect to the directory in your class repository for Homework #3:

cd hw3

Notice the dot

2. Using the command prompt to create the project directory:

django-admin.py startproject webapps .

The “.” at the end of the line above causes the project to be created in the current directory. 17-437/17-637 1/25/2017

Creating a Django Application

Create the calculator application in your project:

1. Run the following Django command to create a new application:

python manage.py startapp calculator

2. Eventhough you’re storing no data in the database, you’ll need to create a database for some of Django’s internal state with the following command:

python manage.py migrate

3. Configure the the project using webapps/settings.py

○

Add 'calculator' to the list of INSTALLED_APPS

4. Configure URL routes using urls.py file(s)

5. Create your templated views in calculator/templates/calculator

6. Put your static files in calculator/static/calculator

7. Create your actions in calculator/views.py

8

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

8. Test your solution (and fix the bugs :-)

python manage.py runserver

Additional resources

See the class examples and the

http://djangoproject.com

website.


do