Enviado por Do utilizador12325

Aula 1

Propaganda
Aula 1
1 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
Aula 1
Primeiro é necessário a criação do ambiente virtual, por isso vamos usar.
python3 -m venv venv
Após isso é necessários ativar o ambiente virtual para instalarmos as
bibliotecas necessárias.
#LINUX source venv/bin/activate #WINDOWS venv\Scripts
\activate.bat #OU venv\Scripts\Activate.ps1
Com o ambiente ativo só nos falta instalar as bibliotecas.
pip install django pillow
Agora precisamos criar a estrutura base do projeto Django, ou seja
devemos utilizar.
django-admin startproject plataforma_cursos .
Vamos configurar o nosso arquivo settings.py
""" Django settings for plataforma_cursos project. Generated by 'djangoadmin startproject' using Django 3.2.6. For more information on this
see https://docs.djangoproject.com/en/3.2/topics/settings/ For the full
list of settings and their values, see https://docs.djangoproject.com
/en/3.2/ref/settings/ """ from pathlib import Path import os # Build paths
inside the project like this: BASE_DIR / 'subdir'. BASE_DIR =
Path(__file__).resolve().parent.parent # Quick-start development settings
unsuitable for production # See https://docs.djangoproject.com/en/3.2
05/10/2021 10:06
Aula 1
2 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF
'plataforma_cursos.urls' TEMPLATES = [ { 'BACKEND':
'django.template.backends.django.DjangoTemplates', 'DIRS':
[os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS':
'context_processors': [ 'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', ], }, }, ]
WSGI_APPLICATION = 'plataforma_cursos.wsgi.application' # Database #
https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES
'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR
'db.sqlite3', } } # Password validation # https://docs.djangoproject
/en/3.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS
{ 'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'
}, { 'NAME':
'django.contrib.auth.password_validation.MinimumLengthValidator', }
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'
}, { 'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
Internationalization # https://docs.djangoproject.com/en/3.2/topics
LANGUAGE_CODE = 'pt-BR' TIME_ZONE = 'America/Sao_Paulo' USE_I18N = True
USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images
https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL
'/static/' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'templates/static'
STATIC_ROOT = os.path.join('static') MEDIA_ROOT =
os.path.join(BASE_DIR,'media') MEDIA_URL = '/media/' # Default primary key
field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default
auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
05/10/2021 10:06
Aula 1
3 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
document_root=settings.MEDIA_ROOT)
O próximo passo é criar as URL dentro de usuário.
from django.urls import path from . import views urlpatterns = [
path('cadastro/', views.cadastro, name = 'cadastro'),
path('login/', views.login, name = 'login'), ]
Criando as respectivas funções temos:
from django.http.response import Http404 from django.shortcuts
import render from django.http import HttpResponse def
cadastro(request): return HttpResponse('cadastro') def
login(request): return HttpResponse('login')
Mas agora precisamos renderizar templates HTML.
Vamos então criar um template base.
<!doctype html> <html lang="pt-BR"> <head> <meta
charset="utf-8"> <meta name="viewport" content="width=devicewidth, initial-scale=1, shrink-to-fit=no"> <link
05/10/2021 10:06
Aula 1
4 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
class="btn btn-success btn-lg" type="submit"> <a href="#"
class="btn btn-info btn-lg">Logar</a> </form> </div> <div
class="col-md-3"> </div> </div> </div> {% endblock %}
Agora só precisamos alterar a view
def cadastro(request): return render(request, 'cadastro.html')
Já estamos renderizando uma página HTML para o cadastro, vamos
repetir o processo para a página de login.
{% extends 'base.html'%} {% block 'titulo'%} Logar {% endblock%}
{% block 'body' %} <div class="container"> <div class="row">
<div class="col-md-3"> </div> <div class="col-md"> <br>
<h1>Login</h1> <hr> <form method="POST" action="#">
05/10/2021 10:06
Aula 1
5 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
Vamos então criar um superuser
python3 manage.py createsuperuser
Aprimorando a área administrativa.
05/10/2021 10:06
Aula 1
6 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
def cadastro(request): status = request.GET.get('status') return
render(request 'cadastro.html' {'status': status})
05/10/2021 10:06
Aula 1
7 of 7
https://grizzly-amaranthus-f6a.notion.site/Aula-1-9e0607f356c94106819...
05/10/2021 10:06
Download