38
Kathmandu 2016 NOW is better than

Django girls-ktm

Embed Size (px)

Citation preview

Kathmandu2016

NOW is better

thanNEVER

Why Python

• 4th Most popular language - IEEE survey• Python is easy to use• Powerful• Versatile• Choice for beginners and experts

Java & Python

TOP 10 PROGRAMMING LANGUAGES

• Java• C• C++• Python• C#• R• PHP• JavaScript• Ruby• Matlab

Django!

Build with Python / Django

A framework

Not a CMS

Overview

Model View Controller

Fat Models

Templates

Thin views

Why Django is awesome

3 reasons

Why Django is awesome

Python

Python

Design Philosophy

- Less Code- Quick development- Don’t repeat yourself (DRY)- Models- URL design- Template system- Views

Community

from django.db import models

class Author(models.Model): name= models.CharField(max_length=50) email = models. EmailField()

class Book(models.Model): author = models.ForeignKey(Author) title= models.CharField(max_length=100) price = models. DecimalField(decimal_places=2, max_digits=11)

from django.conf.urls import patternsfrom books.views import BooksListView, BooksDetailsViewurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^books/$', BooksListView.as_view() ), url(r'^books/(?P<pk>[0-9])/$', BooksDetailsView.as_view() ),]

from django.http import HttpResponsefrom django.views.generic.list import ListViewfrom django.views.generic.detail import DetailsViewfrom .models import Books

class BooksListView(ListView): model = Booksclass BooksListView (DetailsView):

model = Books

models.py

urls.py

views.py

Example, part 1

<html><head><title>Books List</title></head><body>

<ul>{% for book in object_list %}<li><a href="/books/{{ book.id }}"> {{ book.title }}</a> => {{ book.price }} | Author:

{{ book.author.name }} </li>{% endfor %}

</ul>

</body></html>

books_list.html Example, part 1

<html><head><title>Book Details</title></head><body>

<h1>{{ object.title }}</h1> <p>By author: {{ object.author.name }} {{ object.author.email }},</p>

<ul> {% for related_book in object.author.books.all %} <li>{{ related_book }}</li> {% endfor %} </ul>

{% if book.number_of_pages > 100 %} <p>This is a thick book.</p> {% else %} <p>You can read it in 2 hours.</p> {% endif %}

</body></html>

books_detail.html Example, part 1

from django.contrib import adminfrom .models import Bookclass BooksAdmin(admin.ModelAdmin):

list_display = ['title', 'price', 'author']

admin.site.register(Books, BooksAdmin)

admin.py

Admin

Great tools

Happy coding

Resource

• https://jacobian.org/writing/why-django/• https://www.youtube.com/watch?v=OjCz8yTloUI• https://www.youtube.com/watch?

v=n8KnFywpXOE• https://docs.djangoproject.com/en/1.9/• https://www.python.org/• https://www.shuup.com/en/blog/25-of-the-

most-popular-python-and-django-websites/