├── .gitignore ├── README.md ├── core ├── __init__.py ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py ├── manage.py ├── myapp ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ └── __init__.py ├── models.py ├── templates │ ├── base.html │ └── index.html ├── tests.py ├── urls.py └── views.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /tmp 2 | passenger_wsgi.py 3 | .venv 4 | db.sqlite3 5 | /static_media 6 | static_media 7 | /static_files 8 | static_files 9 | /media 10 | mydatabase 11 | file_name.sql 12 | __pycache__ 13 | __pycache__/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django Configuração de Projeto Padrão (Simples) 2 | 3 | Essa configuração simples de projeto django que utilizo para fazer testes e criar aplicações locais. 4 | 5 | Espero que ajude !!! 6 | 7 | Esse é link do Vídeo Tutorial [Link](https://www.youtube.com/watch?v=0y5YdiK7x0k) 8 | 9 | **Configurações Iniciais** 10 | 11 |
Ambiente Virtual Linux/Windows 12 | 13 | - **Ambiente Virtual Linux/Windows** 14 | 15 | 16 | Lembrando… Precisa ter Python instalado no seu ambiente. 17 | 18 | **Criar o ambiente virtual Linux/Windows** 19 | 20 | ```python 21 | ## Windows 22 | python -m venv .venv 23 | source .venv/Scripts/activate # Ativar ambiente 24 | 25 | ## Linux 26 | ## Caso não tenha virtualenv. "pip install virtualenv" 27 | virtualenv .venv 28 | source .venv/bin/activate # Ativar ambiente 29 | ``` 30 | 31 | Instalar os seguintes pacotes. 32 | 33 | ```python 34 | pip install django 35 | pip install pillow 36 | ``` 37 | 38 | Para criar o arquivo *requirements.txt* 39 | 40 | ```python 41 | pip freeze > requirements.txt 42 | ``` 43 | 44 |
45 | 46 |
Criando o Projeto 47 | 48 | - **Criando o Projeto** 49 | 50 | ## **Criando o projeto** 51 | 52 | “core” é nome do seu projeto e quando colocamos um “.” depois do nome do projeto significa que é para criar os arquivos na raiz da pasta. Assim não cria subpasta do projeto. 53 | 54 | ```python 55 | django-admin startproject core . 56 | ``` 57 | 58 | **Testar a aplicação** 59 | 60 | ```python 61 | python manage.py runserver 62 | ``` 63 | 64 |
65 | 66 |
Configurar Settings e Arquivos Static 67 | 68 | - **Configurar Settings e Arquivos Static** 69 | 70 | ## **Vamos configurar nossos arquivos** *static* 71 | 72 | ```python 73 | import os 74 | 75 | # base_dir config 76 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 77 | TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') 78 | STATIC_DIR=os.path.join(BASE_DIR,'static') 79 | 80 | # Database 81 | DATABASES = { 82 | 'default': { 83 | 'ENGINE': 'django.db.backends.sqlite3', 84 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 85 | } 86 | } 87 | 88 | STATIC_ROOT = os.path.join(BASE_DIR,'static') 89 | STATIC_URL = '/static/' 90 | 91 | MEDIA_ROOT=os.path.join(BASE_DIR,'media') 92 | MEDIA_URL = '/media/' 93 | 94 | # Internationalization 95 | # Se quiser deixar em PT BR 96 | LANGUAGE_CODE = 'pt-br' 97 | TIME_ZONE = 'America/Sao_Paulo' 98 | USE_I18N = True 99 | USE_L10N = True 100 | USE_TZ = True 101 | ``` 102 | 103 | *myapp/urls.py* 104 | 105 | ```python 106 | from django.contrib import admin 107 | from django.conf import settings 108 | from django.conf.urls.static import static 109 | from django.urls import path 110 | 111 | urlpatterns = [ 112 | path('admin/', admin.site.urls), 113 | ] 114 | 115 | urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # Adicionar Isto 116 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # Adicionar Isto 117 | ``` 118 |
119 | 120 |
Criando Aplicativo 121 | 122 | - **Criando Aplicativo** 123 | 124 | **Vamos criar nosso aplicativo no Django.** 125 | 126 | Para criar a aplicação no Django rode comando abaixo. “myapp” é nome do seu App. 127 | 128 | ```python 129 | python manage.py startapp myapp 130 | ``` 131 | 132 | Agora precisamos registrar nossa aplicação no *INSTALLED_APPS* localizado em *settings.py*. 133 | 134 |
135 | 136 |
Template base e Bootstrap Configuração 137 | 138 | - **Template base e Bootstrap Configuração** 139 | 140 | ### Bootstrap configuração 141 | 142 | Doc: [https://getbootstrap.com/docs/5.2/getting-started/introduction/](https://getbootstrap.com/docs/5.2/getting-started/introduction/) 143 | 144 | Com Base na documentação para utilizar os recursos Boostrap basta adicionar as tags de CSS e JS. No HTML da Pagina Base. 145 | 146 | ```python 147 | 148 | 149 | 150 | 151 | 152 | ``` 153 | 154 | ## Template Base 155 | 156 | 1 - criar um arquivo base ***base.html*** onde vamos renderizar nosso conteúdo. 157 | 158 | ```python 159 | {% load static %} 160 | 161 | 162 | 163 | 164 | 165 | 166 | {% block title %}{% endblock %} 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | {% block content %} 175 | 176 | {% endblock %} 177 | 178 | 179 | 180 | 181 | 182 | ``` 183 | 184 |
185 | 186 |
Cria uma View 187 | 188 | - **Cria uma View** 189 | 190 | *index1.html* 191 | 192 | ```html 193 | {% extends 'base.html' %} 194 | {% block title %}Pagina Inicial{% endblock %} 195 | {% block content %} 196 |

Pagina Inicial

197 | {% endblock %} 198 | ``` 199 | 200 | *myapp/views.py* 201 | 202 | ```python 203 | from django.shortcuts import render 204 | 205 | # Create your views here. 206 | def mysite(request): 207 | return render(request, 'index1.html') 208 | ``` 209 | 210 | criar arquivo *myapp*/*urls.py* 211 | 212 | ``` 213 | from django.urls import path 214 | from myapp import views 215 | 216 | urlpatterns = [ 217 | path('', views.mysite, name='mysite'), 218 | ] 219 | ``` 220 | 221 | urls.py do projeto. ***core/urls.py*** 222 | 223 | ```python 224 | from django.contrib import admin 225 | from django.urls import path, include # adicionar include 226 | from django.conf import settings 227 | from django.conf.urls.static import static 228 | 229 | urlpatterns = [ 230 | path('admin/', admin.site.urls), 231 | path('', include('myapp.urls')), # url do app 232 | ] 233 | urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # Adicionar Isto 234 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # Adicionar Isto 235 | ``` 236 | 237 | Rodar o projeto para ver como está. 238 | 239 | ```python 240 | python manage.py makemigrations && python manage.py migrate 241 | python manage.py runserver 242 | ``` 243 | 244 | .gitignore 245 | 246 | ```python 247 | /tmp 248 | passenger_wsgi.py 249 | .venv 250 | db.sqlite3 251 | /static_media 252 | static_media 253 | /static_files 254 | static_files 255 | /media 256 | mydatabase 257 | file_name.sql 258 | __pycache__ 259 | __pycache__/ 260 | ``` 261 | 262 |
263 | -------------------------------------------------------------------------------- /core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opencodigos/DjangoProjetoConfiguracao/c9a095051ca8b3cdbbcc5fbaf683094851a893a7/core/__init__.py -------------------------------------------------------------------------------- /core/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for core project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /core/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for core project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | import os 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') 18 | STATIC_DIR=os.path.join(BASE_DIR,'static') 19 | 20 | 21 | # Quick-start development settings - unsuitable for production 22 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 23 | 24 | # SECURITY WARNING: keep the secret key used in production secret! 25 | SECRET_KEY = 'django-insecure-6+hu2r%xgg4tt75r_c3d_b@ri8#7_3t47#y4b++seht=8$x_r6' 26 | 27 | # SECURITY WARNING: don't run with debug turned on in production! 28 | DEBUG = True 29 | 30 | ALLOWED_HOSTS = [] 31 | 32 | 33 | # Application definition 34 | 35 | INSTALLED_APPS = [ 36 | 'django.contrib.admin', 37 | 'django.contrib.auth', 38 | 'django.contrib.contenttypes', 39 | 'django.contrib.sessions', 40 | 'django.contrib.messages', 41 | 'django.contrib.staticfiles', 42 | 'myapp', 43 | ] 44 | 45 | MIDDLEWARE = [ 46 | 'django.middleware.security.SecurityMiddleware', 47 | 'django.contrib.sessions.middleware.SessionMiddleware', 48 | 'django.middleware.common.CommonMiddleware', 49 | 'django.middleware.csrf.CsrfViewMiddleware', 50 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 51 | 'django.contrib.messages.middleware.MessageMiddleware', 52 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 53 | ] 54 | 55 | ROOT_URLCONF = 'core.urls' 56 | 57 | TEMPLATES = [ 58 | { 59 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 60 | 'DIRS': [], 61 | 'APP_DIRS': True, 62 | 'OPTIONS': { 63 | 'context_processors': [ 64 | 'django.template.context_processors.debug', 65 | 'django.template.context_processors.request', 66 | 'django.contrib.auth.context_processors.auth', 67 | 'django.contrib.messages.context_processors.messages', 68 | ], 69 | }, 70 | }, 71 | ] 72 | 73 | WSGI_APPLICATION = 'core.wsgi.application' 74 | 75 | 76 | # Database 77 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 78 | 79 | DATABASES = { 80 | 'default': { 81 | 'ENGINE': 'django.db.backends.sqlite3', 82 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 83 | } 84 | } 85 | 86 | 87 | 88 | # Password validation 89 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 90 | 91 | AUTH_PASSWORD_VALIDATORS = [ 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 100 | }, 101 | { 102 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 103 | }, 104 | ] 105 | 106 | 107 | # Internationalization 108 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 109 | 110 | LANGUAGE_CODE = 'pt-br' 111 | TIME_ZONE = 'America/Sao_Paulo' 112 | USE_I18N = True 113 | USE_L10N = True 114 | USE_TZ = True 115 | 116 | # Static files (CSS, JavaScript, Images) 117 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 118 | 119 | STATIC_ROOT = os.path.join(BASE_DIR,'static') 120 | STATIC_URL = '/static/' 121 | 122 | MEDIA_ROOT=os.path.join(BASE_DIR,'media') 123 | MEDIA_URL = '/media/' 124 | 125 | # Default primary key field type 126 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 127 | 128 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 129 | -------------------------------------------------------------------------------- /core/urls.py: -------------------------------------------------------------------------------- 1 | """core URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/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, include # adicionar include 18 | from django.conf import settings 19 | from django.conf.urls.static import static 20 | 21 | urlpatterns = [ 22 | path('admin/', admin.site.urls), 23 | path('', include('myapp.urls')), # url do app 24 | ] 25 | urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # Adicionar Isto 26 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # Adicionar Isto -------------------------------------------------------------------------------- /core/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for core 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/4.1/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', 'core.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /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 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /myapp/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opencodigos/DjangoProjetoConfiguracao/c9a095051ca8b3cdbbcc5fbaf683094851a893a7/myapp/__init__.py -------------------------------------------------------------------------------- /myapp/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /myapp/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MyappConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'myapp' 7 | -------------------------------------------------------------------------------- /myapp/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opencodigos/DjangoProjetoConfiguracao/c9a095051ca8b3cdbbcc5fbaf683094851a893a7/myapp/migrations/__init__.py -------------------------------------------------------------------------------- /myapp/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /myapp/templates/base.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 | 5 | 6 | 7 | 8 | {% block title %}{% endblock %} 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | {% block content %} 17 | 18 | {% endblock %} 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /myapp/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Pagina Inicial{% endblock %} 4 | 5 | {% block content %} 6 |

Pagina Inicial

7 | {% endblock %} -------------------------------------------------------------------------------- /myapp/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /myapp/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from myapp import views 3 | 4 | urlpatterns = [ 5 | path('', views.mysite, name='mysite'), 6 | ] -------------------------------------------------------------------------------- /myapp/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | def mysite(request): 5 | return render(request, 'index.html') -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.5.2 2 | Django==4.1.3 3 | Pillow==9.3.0 4 | sqlparse==0.4.3 5 | tzdata==2022.7 6 | --------------------------------------------------------------------------------