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

Similar documents
Linguistic Architecture

CE419 Web Programming. Session 15: Django Web Framework

MIT AITI Python Software Development Lab DJ1:

Accelerating Information Technology Innovation

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

MIT Global Startup Labs México 2013

Django: Views, Templates, and Sessions

정재성

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

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

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

Accelerating Information Technology Innovation

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

Tangent MicroServices Documentation

django cms Documentation

django-osm-field Release 0.3.1

Asynchronous WebSockets using Django

Django by errors. Release 0.2. Sigurd Gartmann

Django urls Django Girls Tutorial

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

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

Quoting Wikipedia, software

Ninja Typers Web Application Design Document By Marvin Farrell C

Moving to a Sustainable Web Development Environment for Library Web Applications

staff Documentation Release 1.2

django-intranet Documentation

Building APIs with Django and Django Rest Framework

web frameworks design comparison draft - please help me improve it focus on Model-View-Controller frameworks

DJOAuth2 Documentation

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

TailorDev Contact Documentation

Lecture 10(-ish) Web [Application] Frameworks

django-rest-framework-datatables Documentation

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

kiss.py Documentation

DDF Documentation. Release Paulo Cheque

django-modeltranslation Documentation

Django starting guide

Graphene Documentation

DISBi Documentation. Release Rüdiger Frederik Busche

Unit 6 IoT Physical Servers, Cloud Offerings & IoT Case Studies. By Mahesh R. Sanghavi

MapEntity Documentation

Code editor Django Girls Tutorial

South Africa Templates.

Django IPRestrict Documentation

django-scaffold Documentation

Accelerating Information Technology Innovation

django-templation Documentation

Lightweight. Django USING REST, WEBSOCKETS & BACKBONE. Julia Elman & Mark Lavin

Django design patterns Documentation

Django Admin Sortable Documentation

django-facetools Documentation

LECTURE 14. Web Frameworks

CSV Importer Documentation

Accelerating Information Technology Innovation

Django File Picker Documentation

django-xross Documentation

The Design of Short Message Server with Linux Machine

django-model-report Documentation

django-audiofield Documentation

Building a Django Twilio Programmable Chat Application

generates scaffolding/framework for models, views

Flask-MongoEngine Documentation

Berner Fachhochschule Haute école spécialisée bernoise Berne University of Applied Sciences 2

Puzzlehunt Server Documentation

Framework approach to building content management systems. Wednesday, February 20, 13

django-photologue Documentation

django-cms-search Documentation

django-getpaid Documentation

South Documentation. Release 1.0. Andrew Godwin

First Django Admin Documentation

Bricks Documentation. Release 1.0. Germano Guerrini

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

Django Service Objects Documentation

django-filter Documentation

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

cmsplugin-blog Release post 0 Øyvind Saltvik

UNIVERSIDAD TÉCNICA DEL NORTE FICA, VOL. 01, NO. 01, SEPTIEMBRE

django-filter Documentation

Django Synctool Documentation

A Sample Approach to your Project

Python Schema Generator Documentation

ska Documentation Release Artur Barseghyan

SagePay payment gateway package for django-oscar Documentation

canary Documentation Release 0.1 Branton K. Davis

What You Need to Know about Python

Django Localized Recurrence Documentation

django-dajaxice Documentation

django-telegram-login Documentation

Accelerating Information Technology Innovation

Easy-select2 Documentation

webkitpony Documentation

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

Django Image Tools Documentation

django-ad-code Documentation

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

Technology modeling with MegaL in software development

Django-Mako-Plus Documentation

Aristotle Metadata Registry Documentation

Django Phantom Theme Documentation

Transcription:

Webdev: Building Django Apps Ryan Fox Andrew Glassman MKE Python

What Django is Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It s free and open source. The Web framework for perfectionists with deadlines.

What Django isn t Lightweight Wordpress React/Angular/Vue.js

Who uses it https://disqus.com/ https://www.instagram.com/ https://www.pinterest.com/ Many others!

Getting Django $ pip install django https://github.com/django/django https://www.djangoproject.com/

Getting Django Use 1.11 now, 2.0 coming soon

Structure of Django Models Views Templates

Structure of a Django project $ django-admin startproject my_project my_project/ manage.py my_project init.py settings.py urls.py wsgi.py

Structure of a Django project $ python manage.py startapp my_app my_project/my_app/ admin.py apps.py init.py migrations init.py models.py tests.py views.py

URLs in Django # urls.py from django.conf.urls import url from. import views urlpatterns = [ # ex: /polls/ url(r'^$', views.index, name='index'), # ex: /polls/5/ url(r'^(?p<question_id>[0-9]+)/$', views.detail, name='detail'), # ex: /polls/5/results/ url(r'^(?p<question_id>[0-9]+)/results/$', views.results, name='results'), # ex: /polls/5/vote/ url(r'^(?p<question_id>[0-9]+)/vote/$', views.vote, name='vote'), ]

URLs in Django urlpatterns = [ url(r'^$', IndexView.as_view(), name='index'), url(r'^about/$', AboutView.as_view(), name='about'), url(r'^faq/$', FaqView.as_view(), name='faq'), url(r'^getting-started/$', GettingStartedView.as_view(), name='getting_started'), url(r'^account/$', AccountView.as_view(), name='account'), url(r'^account/cancel/$', AccountCancelView.as_view(), name='account_cancel'), url(r'^dashboard/$', DashboardView.as_view(), name='dashboard'), url(r'^team/$', TeamView.as_view(), name='team'), url(r'^team/records/$', TeamRecordsView.as_view(), name='team_records'),...

Structure of Django Models Views Templates

Models Corresponds to a thing in your application Each model has a DB table Each instance is one row in the DB

Models # models.py from django.db import models class Question(models.Model): question_text = models.charfield(max_length=200) pub_date = models.datetimefield('date published') class Choice(models.Model): question = models.foreignkey(question, on_delete=models.cascade) choice_text = models.charfield(max_length=200) votes = models.integerfield(default=0)

Model fields Determine data type of each attribute on a model: String, int, datetime, etc... Can specify constraints on each field: unique, not null, max length,

Model field types BooleanField CharField DateField DateTimeField DurationField EmailField FileField FloatField ImageField IntegerField PositiveIntegerField TextField TimeField URLField UUIDField ForeignKey* ManyToManyField* OneToOneField*

Models # models.py from django.db import models class Question(models.Model): question_text = models.charfield(max_length=200) pub_date = models.datetimefield('date published') class Choice(models.Model): question = models.foreignkey(question, on_delete=models.cascade) choice_text = models.charfield(max_length=200) votes = models.integerfield(default=0)

Querying models >>> from blog.models import Blog, Entry >>> all_blogs = Blog.objects.all() >>> all_entries = Entry.objects.all()

Querying models >>> old_entries = Entry.objects.filter(pub_date year=2006) >>> january_entries = old_entries.filter(pub_date month=1) >>> new_entries = Entry.objects.filter(pub_date gte=datetime.date(2017, 1, 1))

Querying models >>> first_entry = Entry.objects.get(pk=1)

Structure of Django Models Views Templates

Views # views.py from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.")

Views # views.py from django.http import HttpResponse from django.shortcuts import render from.models import Question def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/polls.html', context)

Structure of Django Models Views Templates

Templates Skeletons that gets transformed to HTML Composable - can include or be included in sub-templates HTML + Django Template Language

Templates <!DOCTYPE html> <html lang="en"> <head> <title>{% block title %}Default title - can be overridden{% endblock %}</title> <link href="{% static 'css/main.css' %}" rel="stylesheet"> </head> <body> {% block content %}{% endblock %} </body> </html>

Templates <!- polls.html -> {% extends 'index.html' %} {% block content %} {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>no polls are available.</p> {% endif %} {% endblock %}

Forms Allow users to submit information Many form fields correspond to model fields

Forms <form action="/your-name/" method="post"> <label for="your_name">your name: </label> <input id="your_name" type="text" name="your_name" value="{{ current_name }}"> <input type="submit" value="ok"> </form>

Creating forms # forms.py from django import forms class NameForm(forms.Form): first_name = forms.charfield(label='your first name', max_length=100) last_name = forms.charfield(label='your last name', max_length=100)

Rendering forms <!- submit.html -> <form action="/your-name/" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="submit" /> </form>

Using forms # views.py from django.shortcuts import render from django.http import HttpResponseRedirect from.forms import NameForm def get_name(request): if request.method == 'POST': form = NameForm(request.POST) if form.is_valid(): new_person = Person(form.cleaned_data[ first_name ], form.cleaned_data[ last_name ]) new_person.save() return HttpResponseRedirect('/thanks/') else: form = NameForm() return render(request, 'name.html', {'form': form})

So much more Migrations Channels (websockets) GIS Caching Deployment strategies Tons more...

Learning more https://docs.djangoproject.com/en/dev/intro/ https://djangocon.us/ https://www.twoscoopspress.com/products/two-scoops-of-django-1-11 http://www.django-rest-framework.org/ 6000+ packages on PyPI

Thanks! ryan@foxrow.com https://www.meetup.com/mke-python-meetup/