MIT AITI Python Software Development Lab DJ1:

Similar documents
정재성

Quoting Wikipedia, software

Django: Views, Templates, and Sessions

Django by errors. Release 0.2. Sigurd Gartmann

MapEntity Documentation

DJOAuth2 Documentation

Graphene Documentation

django-scaffold Documentation

Django urls Django Girls Tutorial

Linguistic Architecture

Tangent MicroServices Documentation

Django Phantom Theme Documentation

open-helpdesk Documentation

Django_template3d Documentation

django-templation Documentation

Webdev: Building Django Apps. Ryan Fox Andrew Glassman MKE Python

Technology modeling. Ralf Lämmel Software Languages Team University of Koblenz-Landau

django-intranet Documentation

Building a Django Twilio Programmable Chat Application

Django starting guide

Django Part II SPARCS 11 undead. Greatly Inspired by SPARCS 10 hodduc

Ninja Typers Web Application Design Document By Marvin Farrell C

Accelerating Information Technology Innovation

Reminders. Full Django products are due next Thursday! CS370, Günay (Emory) Spring / 6

alphafilter Documentation

LFC - Lightning Fast CMS Documentation

The Django Web Framework Part II. Hamid Zarrabi-Zadeh Web Programming Fall 2013

Moving to a Sustainable Web Development Environment for Library Web Applications

django SHOP Release dev0

django-oscar-paypal Documentation

django-model-report Documentation

Djam Documentation. Release Participatory Culture Foundation

Contents. 1. What is otree? 2. The Shell and Python. 3. Example: simple questionnaire. 4. Example: public goods game. 5. Test bots.

django-openid Documentation

Lecture 10(-ish) Web [Application] Frameworks

django-messages Documentation

Accelerating Information Technology Innovation

First Django Admin Documentation

BriCS. University of Bristol Cloud Service Simulation Runner. User & Developer Guide. 1 October John Cartlidge & M.

django-renderit Documentation

django cms Documentation

widgets, events, layout loosely similar to Swing test browser, or plugin for testing with real browser on local system

Rapid Development with Django and App Engine. Guido van Rossum May 28, 2008

Tomasz Szumlak WFiIS AGH 23/10/2017, Kraków

Django File Picker Documentation

Accelerating Information Technology Innovation

generates scaffolding/framework for models, views

TailorDev Contact Documentation

Django PAM Documentation

Django Admin Sortable Documentation

Django Web Framework: A Comprehensive Introduction and Step by Step Installation

django-baton Documentation

Easy-select2 Documentation

Django File Picker Documentation

django-cms-search Documentation

Asynchronous WebSockets using Django

django-facetools Documentation

django-sekizai Documentation

webcore Documentation

django-embed-video Documentation

Django Image Tools Documentation

django-avatar Documentation

Web Development with Python and Django. Mike Crute Mike Pirnat David Stanek

Python. Django. (Web Application Framework - WAF) PyCharm. (Integration Development Environment - IDE)

django-avatar Documentation

django-photologue Documentation

Back-end development. Outline. Example API: Chatter. 1. Design an example API for Chatter. Tiberiu Vilcu.

django-baton Documentation

Accelerating Information Technology Innovation

Building APIs with Django and Django Rest Framework

Django MFA Documentation

Technology modeling. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Python Schema Generator Documentation

django-amp-tools Documentation Release latest

django-dajaxice Documentation

Django OAuth Toolkit Documentation

A Sample Approach to your Project

Technology modeling with MegaL in software development

Django design patterns Documentation

django-permission Documentation

Django Groups Manager Documentation

molly Documentation Release University of Oxford

django-audit-log Documentation

django-audiofield Documentation

Diving into Big Data Workshop

Django Groups Manager Documentation

django-embed-video Documentation

silk Documentation Release 0.3 Michael Ford

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. Django

django-autocomplete-light Documentation

canary Documentation Release 0.1 Branton K. Davis

Django-frontend-notification Documentation

django-inplaceedit Documentation

cmsplugin-blog Release post 0 Øyvind Saltvik

Tango with Django. Chapter 1: Overview. 1. Overview. 2. The N tier architecture of web applications

ska Documentation Release Artur Barseghyan

SAURASHTRA UNIVERSITY

staff Documentation Release 1.2

CE419 Web Programming. Session 15: Django Web Framework

Project OpenDSA Log Support Final Report

Transcription:

MIT AITI Python Software Development Lab DJ1: This lab will help you get Django installed and write your first application. 1 Each person in your group must complete this lab and have it checked off. Make sure that you are running Ubuntu. Do NOT use Eclipse until the end of the lab. All file editing will be done in gedit. 1. Download Django. Follow option 1 here: https://www.djangoproject.com/download/ a. Note that you have to cd (change directories in a terminal) into your directory of choice (probably ~/Downlaods) before running the three listed commands: tar xzvf Django-1.3.tar.gz cd Django-1.3 sudo python setup.py install 2. Navigate to your Desktop $ cd ~/Desktop 3. Create a new project called webnotes : $ django-admin.py startproject webnotes 4. Change to the webnotes directory and look at the list of files. You should see 4 new files in the directory. $ cd webnotes 5. Make a new directory called templates $ mkdir templates 6. We are now going to make a base template for our site. Create an html file called base.html inside the templates folder, and then make a notes directory. $ cd templates $ mkdir notes $ gedit base.html 7. Place the following text into base.html <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 1 This lab was adapted from: http://komunitasweb.com/2010/02/django-tutorial-simple-notes-application/.

<title>webnotes</title> </head> <body> <div id="content"> </div> </body> </html> 8. Change into the notes directory and add two files: detail.html and list.html. These files will extend the base.html file that you have created, by overriding the content section. $ cd notes $ gedit detail.html {% extends 'base.html' %} <h2>{{ note.title }}</h2> <p>{{ note.content }}</p> $ gedit list.html {% extends 'base.html' %} {% if note_list %} <ul> {% for note in note_list %} <li><a href="{% url notes_detail note.id %}">{{ note.title }}</a></li> {% endfor %} </ul> {% else %} You don't have webnotes {% endif %} 9. Edit the settings.py file so that django knows where to look for your templates: $ cd ~/Desktop/webnotes $ gedit settings.py a. At the top of the file, add these two lines: import os # put this at the top of the file PROJECT_ROOT = os.path.realpath(os.path.dirname( file )) b. find the line that starts with TEMPLATE_DIRS and replace it with: TEMPLATE_DIRS = ( os.path.join(project_root, 'templates')

) 10. Keep settings.py open and edit the database configuration, by specifying sqlite3. Find the following two variables and edit them to have these values: ENGINE : 'django.db.backends.sqlite3', NAME : 'webnotes.db', 11. Now start (create) a new application inside this webnotes project: $ django-admin.py startapp notes 12. The application will be placed in a new folder called notes. Navigate into it and look at the list of files. $ cd notes 13. Open models.py and add a notes model $ gedit models.py from django.db import models class Notes(models.Model): title = models.charfield(max_length=255) content = models.textfield() def unicode (self): return self.title 14. Open views.py and add a list and detail view. These views define what data will be displayed and how it will be displayed. $ gedit views.py from django.template import Context, loader from django.http import HttpResponse from models import Notes def notes_list(request): note_list = Notes.objects.all() t = loader.get_template('notes/list.html') c = Context({ 'note_list': note_list, }) return HttpResponse(t.render(c)) def notes_detail(request, id): note = Notes.objects.get(pk=id) t = loader.get_template('notes/detail.html') c = Context({ 'note': note,

}) return HttpResponse(t.render(c)) 15. Create a new file called urls.py and define some URLs that will access your views: $ gedit urls.py from django.conf.urls.defaults import * import views urlpatterns = patterns('', url(r'^list/$', views.notes_list, name='notes_list'), url(r'^detail/(?p<id>\d+)/$', views.notes_detail, name='notes_detail'), ) 16. Create a new file called admin.py and register the notes application with the builtin admin application $ gedit admin.py from notes.models import Notes from django.contrib import admin admin.site.register(notes) 17. Navigate up one directory to get back to your project home. Open the settings.py file and go to the INSTALLED_APPS section. Add notes and uncomment the django.contrib.admin app. $ cd.. $ gedit settings.py INSTALLED_APPS = ( 'notes', 'django.contrib.admin', ) 18. Open the urls.py for your project (not the one for the notes app). Tell your project urls.py that it should include the URLs from your notes app. Uncomment the reference to the admin urls. $ gedit urls.py from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^notes/', include('notes.urls')),

url(r'^admin/', include(admin.site.urls)), 19. Tell django to build a database based on your models. $ python manage.py syncdb When asked to make a superuser, say yes and enter a username, email, and password. DO NOT FORGET THIS. 20. Run the development server $ python manage.py runserver 21. visit http://localhost:8000/admin, and add a note 22. visit http://localhost:8000/notes/list and see if your note is there 23. Once all of this is done, it's time to make a change. Add an author to the notes application and output it in the detail page. Here's how to do it: a. Open models.py $ gedit ~/Desktop/webnotes/notes/models.py b. Add an author field. Hint: it's very similar to the title field. c. Edit the details template $ gedit ~/Desktop/webnotes/templates/notes/detail.html d. Output the author after the title in an <h3> tag e. Go to the terminal where you ran python manage.py runserver, press ctrl+c to stop it f. Delete all previous data so that you have a clean database. $ python manage.py reset notes g. Synchronize the database $ python manage.py syncdb h. Start the server again $ python manage.py runserver i. If your change worked, you should see an author field when adding notes in the admin interface