├── README.md └── mylibrary ├── .gitignore ├── bookshelf ├── __init__.py ├── forms.py ├── migrations │ ├── 0001_initial.py │ └── __init__.py ├── models.py ├── old_views.py ├── templates │ ├── my-author-create.html │ ├── my-book-create.html │ ├── my-book-detail.html │ ├── my-book-edit.html │ ├── my-books.html │ └── welcome.html ├── tests.py └── views.py ├── manage.py ├── mylibrary ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # Example on how to make things with Class Based Views 2 | 3 | Longer explanation (pt-br) in [here](https://medium.com/@leportella/class-based-views-no-django-d76b01ed644e) 4 | 5 | TLDR: 6 | * views.py exemplifies how to make things using Class Based Views 7 | * old_views.py exemplifies same urls but with functions 8 | 9 | 10 | 11 | ## Installation 12 | 13 | This project was updated to Python 3.7 14 | 15 | 16 | ``` 17 | $ pip install -r my_library/requirements.txt 18 | ``` 19 | 20 | 21 | ## Running the project 22 | 23 | To run the server of the project you can use: 24 | 25 | ``` 26 | $ python my_library/manage.py runserver 27 | ``` 28 | 29 | You should apply migrations to the database, so we can have the tables we need: 30 | 31 | ``` 32 | $ python my_library/manage.py migrate 33 | ``` 34 | 35 | 36 | ## URLs available 37 | 38 | 39 | * admin/ 40 | * welcome/ 41 | 42 | Endpoints defined by `views.py` 43 | * my-books/ 44 | * book-detail-by-id/ 45 | * book-detail-by-code/ 46 | * book-update/ 47 | * book-create 48 | * author-create 49 | 50 | Endpoints defined by `old_views.py` 51 | * old/welcome/ 52 | * old/my-books/ 53 | * old/book-detail-by-id/ 54 | * old/book-detail-by-code/ 55 | * old/book-update/ 56 | 57 | -------------------------------------------------------------------------------- /mylibrary/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *$py.class 4 | *.pyc 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | 93 | # Rope project settings 94 | .ropeproject 95 | 96 | # Custom ignore 97 | 98 | *.sqlite3 99 | .idea/ 100 | *.swp 101 | **/staticfiles/** 102 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leportella/class-based-views-example/e7f50213cde0e4282083bddc4685a2cfbeec3b83/mylibrary/bookshelf/__init__.py -------------------------------------------------------------------------------- /mylibrary/bookshelf/forms.py: -------------------------------------------------------------------------------- 1 | from django.forms import ModelForm 2 | 3 | from .models import Author, Book 4 | 5 | class AuthorForm(ModelForm): 6 | class Meta: 7 | model = Author 8 | fields = ['name'] 9 | 10 | class BookForm(ModelForm): 11 | class Meta: 12 | model = Book 13 | fields = ['title', 'pages', 'code', 'author'] 14 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.11.3 on 2017-09-26 14:33 3 | from __future__ import unicode_literals 4 | 5 | from django.db import migrations, models 6 | import django.db.models.deletion 7 | 8 | 9 | class Migration(migrations.Migration): 10 | 11 | initial = True 12 | 13 | dependencies = [ 14 | ] 15 | 16 | operations = [ 17 | migrations.CreateModel( 18 | name='Author', 19 | fields=[ 20 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 21 | ('name', models.CharField(max_length=250)), 22 | ], 23 | ), 24 | migrations.CreateModel( 25 | name='Book', 26 | fields=[ 27 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 28 | ('title', models.CharField(max_length=250)), 29 | ('pages', models.IntegerField()), 30 | ('code', models.IntegerField()), 31 | ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.Author')), 32 | ], 33 | ), 34 | ] 35 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leportella/class-based-views-example/e7f50213cde0e4282083bddc4685a2cfbeec3b83/mylibrary/bookshelf/migrations/__init__.py -------------------------------------------------------------------------------- /mylibrary/bookshelf/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Author(models.Model): 5 | name = models.CharField(max_length=250) 6 | 7 | def __str__(self): 8 | return self.name 9 | 10 | 11 | class Book(models.Model): 12 | author = models.ForeignKey(Author, on_delete=models.CASCADE) 13 | title = models.CharField(max_length=250) 14 | pages = models.IntegerField() 15 | code = models.IntegerField() 16 | 17 | def __str__(self): 18 | return '%s - %s - %s - %s' % (self.author.name, self.title, self.code, self.pages) 19 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/old_views.py: -------------------------------------------------------------------------------- 1 | from django.http import Http404 2 | from django.shortcuts import redirect, render 3 | 4 | from .forms import BookForm 5 | from .models import Book 6 | 7 | 8 | def welcome(request): 9 | return render(request, 'welcome.html') 10 | 11 | 12 | def get_books_list(request): 13 | books = Book.objects.all() 14 | return render(request, 'my-books.html', {'object_list': books}) 15 | 16 | 17 | def get_book_by_id(request, pk): 18 | if request.method == 'GET': 19 | try: 20 | book = Book.objects.get(id=pk) 21 | except Book.DoesNotExist: 22 | raise Http404("Book does not exist") 23 | return render(request, 'my-book-detail.html', {'object': book}) 24 | 25 | 26 | def get_book_by_code(request, code): 27 | if request.method == 'GET': 28 | try: 29 | book = Book.objects.get(code=code) 30 | except Book.DoesNotExist: 31 | raise Http404("Book does not exist") 32 | return render(request, 'my-book-detail.html', {'object': book}) 33 | 34 | 35 | def edit_book(request, pk): 36 | try: 37 | book = Book.objects.get(id=pk) 38 | except Book.DoesNotExist: 39 | raise Http404("Book does not exist") 40 | 41 | if request.method == 'GET': 42 | form = BookForm(instance=book) 43 | return render(request, 'my-book-edit.html', {'form': form, 'object': book}) 44 | 45 | if request.method == 'POST': 46 | book = Book.objects.get(id=pk) 47 | form = BookForm(request.POST, instance=book) 48 | if form.is_valid(): 49 | form.save() 50 | return redirect('/old/my-books') 51 | 52 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/my-author-create.html: -------------------------------------------------------------------------------- 1 |

This is a author create form

2 | 3 |
4 |
7 | {% csrf_token %} 8 | {{ form }} 9 |
10 |
11 | 12 |
13 |
-------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/my-book-create.html: -------------------------------------------------------------------------------- 1 |

This is a book create form

2 | 3 |
4 |
7 | {% csrf_token %} 8 | {{ form }} 9 |
10 |
11 | 12 |
13 |
-------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/my-book-detail.html: -------------------------------------------------------------------------------- 1 |

Nice! Book detail!

2 | 3 | 4 |

Book title: {{ object.title }} 5 |

Book Code: {{ object.code }} 6 |

Book Id: {{ object.id }} 7 |

Book Author: {{ object.author.name }} 8 |

Book Num of Pages: {{ object.pages }} 9 | 10 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/my-book-edit.html: -------------------------------------------------------------------------------- 1 |

This is a book update form

2 | 3 |
4 |
7 | {% csrf_token %} 8 | {{ form }} 9 |
10 |
11 | 12 |
13 |
14 | 15 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/my-books.html: -------------------------------------------------------------------------------- 1 |

These are some of my books:

2 | 3 |
    4 | {% for book in object_list %} 5 |
  • {{ book }}
  • 6 | {% empty %} 7 |
  • No books yet.
  • 8 | {% endfor %} 9 |
10 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/templates/welcome.html: -------------------------------------------------------------------------------- 1 |

Welcome!!

2 | 3 |

This is my library

4 |

What do you think about it?

5 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /mylibrary/bookshelf/views.py: -------------------------------------------------------------------------------- 1 | from django.views.generic import (CreateView, DetailView, ListView, TemplateView, 2 | UpdateView) 3 | 4 | from .forms import AuthorForm, BookForm 5 | from .models import Author, Book 6 | 7 | 8 | class WelcomeView(TemplateView): 9 | template_name = 'welcome.html' 10 | 11 | class AuthorCreateView(CreateView): 12 | model = Author 13 | template_name = 'my-author-create.html' 14 | form_class = AuthorForm 15 | success_url = '/my-books' 16 | 17 | class BooksView(ListView): 18 | model = Book 19 | template_name = 'my-books.html' 20 | 21 | 22 | class BookDetailByIdView(DetailView): 23 | model = Book 24 | template_name = 'my-book-detail.html' 25 | 26 | 27 | class BookDetailByCodeView(DetailView): 28 | model = Book 29 | template_name = 'my-book-detail.html' 30 | 31 | def get_object(self): 32 | return Book.objects.get(code=self.kwargs['code']) 33 | 34 | 35 | class BookUpdateView(UpdateView): 36 | model = Book 37 | template_name = 'my-book-edit.html' 38 | form_class = BookForm 39 | success_url = '/my-books' 40 | 41 | class BookCreateView(CreateView): 42 | model = Book 43 | template_name = 'my-book-create.html' 44 | form_class = BookForm 45 | success_url = '/my-books' 46 | 47 | -------------------------------------------------------------------------------- /mylibrary/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mylibrary.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /mylibrary/mylibrary/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leportella/class-based-views-example/e7f50213cde0e4282083bddc4685a2cfbeec3b83/mylibrary/mylibrary/__init__.py -------------------------------------------------------------------------------- /mylibrary/mylibrary/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for mylibrary project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.11.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.11/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '2-z3gt=7iu*n4coxzk8mx)$f1=8$eo#d4e@v2ypn$!7tfy%$on' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'django_extensions', 41 | 'bookshelf', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'mylibrary.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'mylibrary.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/1.11/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/1.11/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/1.11/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | -------------------------------------------------------------------------------- /mylibrary/mylibrary/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import url 2 | from django.contrib import admin 3 | 4 | from bookshelf import views 5 | from bookshelf import old_views 6 | 7 | urlpatterns = [ 8 | url(r'^admin/', admin.site.urls), 9 | url(r'^welcome/', views.WelcomeView.as_view()), 10 | url(r'^my-books/', views.BooksView.as_view()), 11 | url(r'^book-detail-by-id/(?P[-\w]+)', 12 | views.BookDetailByIdView.as_view()), 13 | url(r'^book-detail-by-code/(?P[-\w]+)', 14 | views.BookDetailByCodeView.as_view()), 15 | url(r'^book-update/(?P[-\w]+)$', views.BookUpdateView.as_view()), 16 | url(r'^book-create$', views.BookCreateView.as_view()), 17 | url(r'^author-create$', views.AuthorCreateView.as_view()), 18 | 19 | url(r'^old/welcome/', old_views.welcome), 20 | url(r'^old/my-books/', old_views.get_books_list), 21 | url(r'^old/book-detail-by-id/(?P[-\w]+)', 22 | old_views.get_book_by_id), 23 | url(r'^old/book-detail-by-code/(?P[-\w]+)', 24 | old_views.get_book_by_code), 25 | url(r'^old/book-update/(?P[-\w]+)', 26 | old_views.edit_book), 27 | ] 28 | -------------------------------------------------------------------------------- /mylibrary/mylibrary/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for mylibrary project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mylibrary.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /mylibrary/requirements.txt: -------------------------------------------------------------------------------- 1 | Django 2 | django-crispy-forms==1.6.1 3 | django-extensions==1.9.1 4 | ipdb==0.10.3 5 | --------------------------------------------------------------------------------