Last active
March 1, 2021 18:09
-
-
Save dmendiza/1b2d3231c9bfe33c0b8a to your computer and use it in GitHub Desktop.
Django Unchained - PyTexas 2014
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install Django | |
pip install django |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a new Project called "bookstore" | |
django-admin startproject bookstore | |
cd bookstore/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run the development server | |
python manage.py runserver | |
# Django is now serving your project at http://127.0.0.1:8000/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a new application called "inventory" | |
python manage.py startapp inventory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Skip this! This is done differently for 2.0 | |
# | |
# Add the new application to your project | |
# in bookstore/bookstore/settings.py edit INSTALLED_APPS to include 'inventory' | |
INSTALLED_APPS = ( | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
# Not for 2.0 | |
# 'inventory', | |
'inventory.apps.InventoryConfig', | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In bookstore/inventory/views.py add a new view | |
# First import Django’s HTTPResponse | |
from django.http import HttpResponse | |
# Now add the home view we previously mapped in the URLConf | |
def home(request): | |
return HttpResponse( | |
'<html><body><h1>Django Unchained!</h1></body></html>' | |
) | |
# We should now have a new home page that says "Django Unchained!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In bookstore/bookstore/urls.py add a new url pattern | |
# url is obsolete in 2.0 | |
# url(r'^$', 'inventory.views.home'), | |
# In bookstore/inventory/urls.py | |
from django.urls import path | |
from . import views | |
urlpatterns = [ | |
path('', views.home, name='home'), | |
] | |
# Then go to bookstoore/bookstore/urls.py | |
from django.urls import include, path | |
urlpatterns = [ | |
path('/', include('inventory.urls')), | |
path('admin/', admin.site.urls), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In bookstore/inventory/models.py add the following two models | |
class Author(models.Model): | |
first_name = models.CharField(max_length=256) | |
last_name = models.CharField(max_length=256) | |
class Book(models.Model): | |
author = models.ForeignKey(Author) | |
title = models.CharField(max_length=256) | |
release_date = models.DateField(null=True) | |
isbn = models.CharField(max_length=13) | |
list_price = models.DecimalField(max_digits=8, decimal_places=2) | |
on_hand = models.IntegerField(default=0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create and apply the first migration | |
python manage.py makemigraitons | |
python manage.py migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let’s design a view that will give you book details when you navigate to a URL that has the book’s ISBN | |
# In bookstore/bookstore/urls.py add a new url pattern | |
# http://localhost:8000/books/ISBN | |
url(r'^books/(\d+)/$', 'inventory.views.detail'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adding the detail view | |
# In bookstore/inventory/views.py let’s add some imports | |
from django.http import HttpResponse, Http404 | |
from inventory.models import Book | |
# Now let's add the detail view function | |
def detail(request, isbn): | |
matches = Book.objects.filter(isbn__exact=isbn) | |
if not matches: | |
raise Http404 | |
else: | |
book = matches[0] | |
return render(request, 'detail.html', {'book': book}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Create a new directory for templates at bookstore/inventory/templates/ --> | |
<!-- add a new base.html template in the directory we just created --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>{% block title %}{% endblock %}</title> | |
</head> | |
<body> | |
{% block content %} | |
{% endblock %} | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- add a new detail.html template --> | |
{% block title %}Django Bookstore{% endblock %} | |
{% block content %} | |
<div class="book"> | |
<h1>{{ book.title }}</h1> | |
<h2>by {{ book.author.first_name }} {{ book.author.last_name }}</h2> | |
{% if book.release_date %} | |
<p>Released on {{ book.release_date }}</p> | |
{% endif %} | |
</div> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let’s add a new model to save form data | |
class Order(models.Model): | |
first_name = models.CharField(max_length=256) | |
last_name = models.CharField(max_length=256) | |
email = models.EmailField() | |
book = models.ForeignKey(Book) | |
quantity = models.IntegerField(default=1) | |
# Don't forget to migrate | |
# python manage.py makemigrations | |
# python manage.py migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now let’s make a Form from the model | |
# First add a new file bookstore/inventory/forms.py | |
from django import forms | |
from inventory import models | |
class OrderForm(forms.Form): | |
first_name = forms.CharField(max_length=256) | |
last_name = forms.CharField(max_length=256) | |
email = forms.emailField() | |
book = forms.ModelChoiceField(queryset=models.Book.objects.all()) | |
quantity = forms.IntegerField() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now let’s wire up some new URLs and a couple of views to process form data. | |
# In bookstore/bookstore/urls.py add | |
url(r'^books/order/$', 'inventory.views.order'), | |
url(r'^books/thanks/$', 'inventory.views.thanks'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In bookstore/inventory/views.py add | |
from django.http import HttpResponse, Http404, HttpResponseRedirect | |
from inventory.forms import OrderForm | |
from inventory.models import Book, Order | |
def thanks(request): | |
return HttpResponse( | |
'<html><body><h1>Thanks!</h1></body></html' | |
) | |
def order(req): | |
if request.method == 'POST': | |
form = OrderForm(request.POST) | |
if form.is_valid(): | |
order = Order() | |
order.first_name = form.cleaned_data['first_name'] | |
order.last_name = form.cleaned_data['last_name'] | |
order.book = form.cleaned_data['book'] | |
order.quantity = form.cleaned_data['quantity'] | |
order.save() | |
return HttpResponseRedirect('/books/thanks/') | |
else: | |
form = OrderForm() | |
return render(request, 'order.html', {'form': form}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Add a new template in bookstore/inventory/templates/order.html --> | |
{% block title %}Order a book{% endblock %} | |
{% block content %} | |
<form action="/books/order/" method="post"> | |
{% csrf_token %} | |
{{ form.as_p }} | |
<input type=submit> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First let’s add a new user | |
python manage.py createsuperuser | |
# Now let's check out the admin site at http://localhost:8000/admin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's add our models to the Admin site | |
# In bookstore/inventory/admin.py add | |
from inventory import models | |
admin.site.register(models.Book) | |
admin.site.register(models.Author) | |
admin.site.register(models.Order) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment