├── .gitignore ├── Procfile ├── README.md ├── contrib └── env_gen.py ├── manage.py ├── myproject ├── __init__.py ├── core │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── static │ │ └── css │ │ │ └── main.css │ ├── templates │ │ ├── base.html │ │ ├── includes │ │ │ └── nav.html │ │ └── index.html │ ├── tests.py │ └── views.py ├── settings.py ├── urls.py └── wsgi.py ├── requirements.txt └── runtime.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 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 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | *.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn myproject.wsgi --log-file - 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # django-simples 2 | 3 | Exemplo de um projeto simples com Django. 4 | 5 | ## Como rodar o projeto? 6 | 7 | * Clone esse repositório. 8 | * Crie um virtualenv com Python 3. 9 | * Ative o virtualenv. 10 | * Instale as dependências. 11 | * Rode as migrações. 12 | 13 | ``` 14 | git clone https://github.com/rg3915/django-simples.git 15 | cd django-simples 16 | python3 -m venv .venv 17 | source .venv/bin/activate 18 | pip install -r requirements.txt 19 | python contrib/env_gen.py 20 | python manage.py migrate 21 | ``` 22 | 23 | ## Links 24 | 25 | 26 | [djangoproject.com](https://www.djangoproject.com/start/) 27 | 28 | [Tutorial Django 2.2](http://pythonclub.com.br/tutorial-django-2.html) 29 | 30 | [python-decouple](https://github.com/henriquebastos/python-decouple) 31 | 32 | [Live de Python #97 - Desacoplando configurações com Decouple - com Henrique Bastos](https://www.youtube.com/watch?v=zYJGpLw5Wv4) 33 | 34 | [env_gen](https://gist.github.com/rg3915/22626de522f5c045bc63acdb8fe67b24) 35 | 36 | [base.html](https://gist.github.com/rg3915/0144a2408ec54c4e8008999631c64a30) 37 | 38 | [Live de Python #94 - Django básico - com Regis Santos](https://www.youtube.com/watch?v=YuKdwIhJysU) 39 | 40 | [Live de Python #95 - Entendendo o ORM do Django - com Regis Santos](https://www.youtube.com/watch?v=cyxky2QJlwg) 41 | 42 | [Emmet](https://emmet.io/) 43 | 44 | [Heroku](https://www.heroku.com/home) 45 | 46 | [Configuring Django Apps for Heroku](https://devcenter.heroku.com/articles/django-app-configuration) 47 | 48 | [Tutorial deploy no Heroku](https://github.com/rg3915/tutoriais/tree/master/django-basic#deploy-no-heroku) 49 | 50 | [dj-static](https://pypi.org/project/dj-static/) -------------------------------------------------------------------------------- /contrib/env_gen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Django SECRET_KEY generator. 5 | """ 6 | from django.utils.crypto import get_random_string 7 | 8 | 9 | chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' 10 | 11 | CONFIG_STRING = """ 12 | DEBUG=True 13 | SECRET_KEY=%s 14 | ALLOWED_HOSTS=127.0.0.1, .localhost 15 | """.strip() % get_random_string(50, chars) 16 | 17 | # Writing our configuration file to '.env' 18 | with open('.env', 'w') as configfile: 19 | configfile.write(CONFIG_STRING) 20 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /myproject/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rg3915/django-simples/ab5d9cb3b5c54b354d4906ad6e4c49a2e4eda6e8/myproject/__init__.py -------------------------------------------------------------------------------- /myproject/core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rg3915/django-simples/ab5d9cb3b5c54b354d4906ad6e4c49a2e4eda6e8/myproject/core/__init__.py -------------------------------------------------------------------------------- /myproject/core/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /myproject/core/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CoreConfig(AppConfig): 5 | name = 'core' 6 | -------------------------------------------------------------------------------- /myproject/core/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rg3915/django-simples/ab5d9cb3b5c54b354d4906ad6e4c49a2e4eda6e8/myproject/core/migrations/__init__.py -------------------------------------------------------------------------------- /myproject/core/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /myproject/core/static/css/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin-top: 60px; 3 | } 4 | .ok { 5 | color: #44AD41; /*green*/ 6 | } 7 | .no { 8 | color: #DE2121; /*vermelho*/ 9 | } -------------------------------------------------------------------------------- /myproject/core/templates/base.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Django Simples 10 | 11 | 12 | 13 | 14 | 15 | 16 | {% block css %}{% endblock css %} 17 | 18 | 19 | 20 | 21 | 22 | {% include "includes/nav.html" %} 23 | 24 |
25 | {% block content %}{% endblock content %} 26 |
27 | 28 | 29 | 30 | 31 | 32 | 33 | {% block js %}{% endblock js %} 34 | 35 | 36 | -------------------------------------------------------------------------------- /myproject/core/templates/includes/nav.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /myproject/core/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |
6 |
7 |

Bem-vindo ao Django

8 |

Exemplo básico Github

9 |
10 |
11 | 12 |
13 | {% if object_list %} 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | {% for object in object_list %} 25 | 26 | 27 | 28 | 29 | 30 | 31 | {% endfor %} 32 | 33 |
UsuárioNomeSobrenomeE-mail
{{ object.username }}{{ object.first_name }}{{ object.last_name }}{{ object.email }}
34 | {% else %} 35 |

Sem itens na lista

36 | {% endif %} 37 |
38 | 39 | {% endblock content %} -------------------------------------------------------------------------------- /myproject/core/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /myproject/core/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from django.contrib.auth.models import User 3 | 4 | 5 | def index(request): 6 | users = User.objects.all() 7 | context = {'object_list': users} 8 | template_name = 'index.html' 9 | return render(request, template_name, context) 10 | -------------------------------------------------------------------------------- /myproject/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for myproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2.4. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | from decouple import config, Csv 15 | from dj_database_url import parse as dburl 16 | 17 | 18 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 19 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 20 | 21 | 22 | # Quick-start development settings - unsuitable for production 23 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 24 | 25 | # SECURITY WARNING: keep the secret key used in production secret! 26 | # SECRET_KEY = config('SECRET_KEY') 27 | SECRET_KEY = 'jhbKPTA83HAbX9KzOEF60z7PW5hY1apa' 28 | 29 | # SECURITY WARNING: don't run with debug turned on in production! 30 | # DEBUG = config('DEBUG', default=False, cast=bool) 31 | DEBUG = True 32 | 33 | # ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[''], cast=Csv()) 34 | ALLOWED_HOSTS = '*' 35 | 36 | 37 | # Application definition 38 | 39 | INSTALLED_APPS = [ 40 | 'django.contrib.admin', 41 | 'django.contrib.auth', 42 | 'django.contrib.contenttypes', 43 | 'django.contrib.sessions', 44 | 'django.contrib.messages', 45 | 'django.contrib.staticfiles', 46 | # Apps de terceiros 47 | 'django_extensions', 48 | # Minhas apps 49 | 'myproject.core', 50 | ] 51 | 52 | MIDDLEWARE = [ 53 | 'django.middleware.security.SecurityMiddleware', 54 | 'django.contrib.sessions.middleware.SessionMiddleware', 55 | 'django.middleware.common.CommonMiddleware', 56 | 'django.middleware.csrf.CsrfViewMiddleware', 57 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 58 | 'django.contrib.messages.middleware.MessageMiddleware', 59 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 60 | ] 61 | 62 | ROOT_URLCONF = 'myproject.urls' 63 | 64 | TEMPLATES = [ 65 | { 66 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 67 | 'DIRS': [], 68 | 'APP_DIRS': True, 69 | 'OPTIONS': { 70 | 'context_processors': [ 71 | 'django.template.context_processors.debug', 72 | 'django.template.context_processors.request', 73 | 'django.contrib.auth.context_processors.auth', 74 | 'django.contrib.messages.context_processors.messages', 75 | ], 76 | }, 77 | }, 78 | ] 79 | 80 | WSGI_APPLICATION = 'myproject.wsgi.application' 81 | 82 | 83 | # Database 84 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 85 | 86 | default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3') 87 | DATABASES = { 88 | 'default': config('DATABASE_URL', default=default_dburl, cast=dburl), 89 | } 90 | 91 | 92 | # Password validation 93 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 94 | 95 | AUTH_PASSWORD_VALIDATORS = [ 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 101 | }, 102 | { 103 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 104 | }, 105 | { 106 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 107 | }, 108 | ] 109 | 110 | 111 | # Internationalization 112 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 113 | 114 | LANGUAGE_CODE = 'pt-br' 115 | 116 | TIME_ZONE = 'America/Sao_Paulo' 117 | 118 | USE_I18N = True 119 | 120 | USE_L10N = True 121 | 122 | USE_TZ = True 123 | 124 | 125 | # Static files (CSS, JavaScript, Images) 126 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 127 | 128 | STATIC_URL = '/static/' 129 | STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 130 | -------------------------------------------------------------------------------- /myproject/urls.py: -------------------------------------------------------------------------------- 1 | """myproject URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from myproject.core.views import index 19 | 20 | 21 | urlpatterns = [ 22 | path('', index, name='index'), 23 | path('admin/', admin.site.urls), 24 | ] 25 | -------------------------------------------------------------------------------- /myproject/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for myproject 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/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | from dj_static import Cling 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 15 | 16 | application = Cling(get_wsgi_application()) 17 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | dj-database-url==0.5.0 2 | dj-static==0.0.6 3 | Django==2.2.27 4 | django-extensions==2.2.1 5 | gunicorn==19.9.0 6 | psycopg2-binary==2.8.5 7 | python-decouple==3.1 8 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.7.3 2 | --------------------------------------------------------------------------------