Apostila Python 3.8.2 Django
- visual code
- instalar plugins
- phyton para vc code
- kite phyton
- linting, debbugi phyton
- baixar e instalar o phyton
- instalar sem colocar email
- marcar automaticamente
NO LINUX:
ATUALIZANDO A VERSAO DO PIP3
https://tecadmin.net/install-django-on-ubuntu/
Step 3 – Create A Django Application
cd /var/www
django-admin startproject testedom
cd django_app
python3 manage.py migrate
Step 4 – Create Super User
python3 manage.py createsuperuser
Step 5 – Run Django Application
vi django_app/settings.py
ALLOWED_HOSTS = ['192.168.1.239']
python3 manage.py runserver 0.0.0.0:8000
http://192.168.1.239:8000
sh pycharm.sh
NO PYCHARM:
CRIANDO PROJETO
https://www.treinaweb.com.br/blog/criando-o-primeiro-projeto-django/
COMANDO PARA RODAR O DJANGO
python manage.py runserver
RODA NESSA PORTA
http://127.0.0.1:8000/
https://tutorial.djangogirls.org/pt/django_models/
python manage.py startapp blog
criar classe modelo
python manage.py makemigrations blog - criando banco
python manage.py migrate blog - criar as tabelas
alterar admin.py
python manage.py runserver
NO DIRETORIO DO PYCHARM 26-04-2020
CRIAR PROJETO DJANGO
Rodar no terminal:
django-admin startproject testedom
abrir o Pycharm nesse diretorio
COMANDO PARA RODAR O DJANGO
python manage.py runserver
NO WINDOWS
https://medium.com/horadecodar/instalando-django-2-1-e-criando-um-projeto-windows-e-linux-67cbff58496c
CRIAR A ENV
python -m venv myvenv
myvenv\Scripts\activate
python -m pip install –upgrade pip
criar requirements.txt
escrever nele Django~=3.0.5
pip install -r requirements.txt
myvenv\Scripts\activate
django-admin.exe startproject mysite .
python manage.py migrate
python manage.py startapp blog
classe models
python manage.py makemigrations blog
manage.py migrate blog
https://docs.djangoproject.com/en/3.0/intro/tutorial01/
django-admin startproject mysite
python manage.py startapp polls
https://docs.python.org/pt-br/3/tutorial/venv.html
Criar um diretorio, entrar nele e dar os comandos pelo PowerShell administrador
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Digitar no terminal:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Dando o mesmo comando pelo terminal do Visual Code
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Abrir um outro terminal e instalar as bibliotecas:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
(Digitar tudo numa linha só)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Para listar as biblotecas instaladas:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Estrutura do diretorio:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Com o Django instalado vamos iniciar o projeto.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Entrar no diretorio
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Criar o projeto alunos

Comando Migrate para o django criar as nossas tabelas do DB

Criar um usuário para acessar o Django
Exemplo:
Usuario: Belem
Senha: 1234

Iniciar o servidor

Visualizando no browser

Logando como administrador:
http://127.0.0.1:8000/admin

Logando com usuario: Belem e senha: 1234

Logado

Em projeto_belem/projeto_belem/settings.py vamos configurar os INSTALLED_APPS.

Em alunos/models.py
Realizamos as migrations


0001_initial.py
Gerado automaticamente
Migrar o model

projeto_belem/alunos/admin.py
Iniciar o servidor
Visualizando no browser. Mostra alunos
http://127.0.0.1:8000/admin/

Adicionando toString na classe
Confirmar alterações na classe

projeto_belem/projeto_belem/urls.py

projeto_belem/projeto_belem/settings.py
Adicionar ao final da classe

Criar o diretorio api
e os arquivos:
serializers.py
views.py
urls.py
__init__.py

projeto_belem/alunos/api/serializers.py
projeto_belem/alunos/api/views.py
projeto_belem/alunos/api/urls.py
projeto_belem/alunos/api/__init__.py
projeto_belem/projeto_belem/urls.py
Projeto finalizado
http://127.0.0.1:8000/

http://127.0.0.1:8000/admin/

http://127.0.0.1:8000/api/

http://127.0.0.1:8000/api/alunos/

Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Criando projeto many to many
Criar um diretorio para armazenar os projetos
Criar a env e ativar
Fazer as instalações das bibliotecas:
.






Criar o projeto django chamado sistema

Entrar no diretorio criado:

Criar o app.

Projeto criado

Alterar models.py
from django.db import models
class Peca(models.Model):
idPeca = models.AutoField(primary_key= True,)
valor = models.FloatField()
descricao = models.CharField(max_length=255)
def __str__(self):
return '%d, %f, %s' % (self.idPeca, self.valor, self.descricao)
def __repr__(self):
return '%d, %f, %s' % (self.idPeca, self.valor, self.descricao)
pass
class Usuario(models.Model):
idUsuario = models.AutoField(primary_key=True, )
nome = models.CharField(max_length= 100)
def __repr__(self):
return '%d, %s' % (self.idUsuario, self.nome)
def __str__(self):
return '%d, %s' % (self.idUsuario, self.nome)
pass
class Demanda(models.Model):
idDemanda = models.AutoField(primary_key = True, )
rua = models.CharField(max_length=100)
bairro = models.CharField(max_length=100)
numero = models.CharField(max_length=100)
cep = models.CharField(max_length=100)
cidade = models.CharField(max_length=100)
status = models.IntegerField()
usuario = models.ForeignKey(Usuario, on_delete=models.CASCADE)
pecas = models.ManyToManyField(Peca)
def __str__(self):
return '%d, %s, %s, %s, %s, %s, %s' % (self.idDemanda, self.rua, self.bairro, self.numero, self.cep, self.cidade, self.status)
def __repr__(self):
return '%d, %s, %s, %s, %s, %s, %s' % (self.idDemanda, self.rua, self.bairro, self.numero, self.cep, self.cidade, self.status)
pass
Alterar settings.py
, inserindo os apps e o banco de dados
Alterar Urls.py
do projeto (sistema). Deixar comentado para rodar o makemigrations
Fazer a migration


Classe gerada 0001_initial.py
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Peca',
fields=[
('idPeca', models.AutoField(primary_key=True, serialize=False)),
('valor', models.FloatField()),
('descricao', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Usuario',
fields=[
('idUsuario', models.AutoField(primary_key=True, serialize=False)),
('nome', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Demanda',
fields=[
('idDemanda', models.AutoField(primary_key=True, serialize=False)),
('rua', models.CharField(max_length=100)),
('bairro', models.CharField(max_length=100)),
('numero', models.CharField(max_length=100)),
('cep', models.CharField(max_length=100)),
('cidade', models.CharField(max_length=100)),
('status', models.IntegerField()),
('pecas', models.ManyToManyField(to='restEstoque.Peca')),
('usuario', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='restEstoque.Usuario')),
],
),
]
Criar o admin do projeto. Usuario: belem, senha: 123

Dentro de sistema criar o diretorio api

Dentro de restEstoque criar o arquivo serializers.py
Alterar a views.py
do rest_projeto
Alterar urls.py
dentro do restEstoque
Alterar urls.py
de sistema. Descomentar
Para rodar:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Visualizando no banco
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
tags: python
apostila