Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
CRUD_Animais_da_Fazenda2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Hiago Rafael
CRUD_Animais_da_Fazenda2
Commits
7c78a829
Commit
7c78a829
authored
May 07, 2026
by
bricioso
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
atualizei
parent
ec7e34f2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
29 deletions
+25
-29
models.cpython-314.pyc
__pycache__/models.cpython-314.pyc
+0
-0
app.py
app.py
+25
-29
fazenda.db
fazenda.db
+0
-0
No files found.
__pycache__/models.cpython-314.pyc
View file @
7c78a829
No preview for this file type
app.py
View file @
7c78a829
from
flask
import
Flask
,
render_template
,
request
,
redirect
from
flask
import
Flask
,
render_template
,
request
,
redirect
,
url_for
from
flask_sqlalchemy
import
SQLAlchemy
from
models
import
db
,
Animal
import
os
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
# CONFIGURAÇÃO DO BANCO
# Configuração do banco de dados real
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'sqlite:///fazenda.db'
basedir
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
))
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'sqlite:///'
+
os
.
path
.
join
(
basedir
,
'fazenda.db'
)
app
.
config
[
'SQLALCHEMY_TRACK_MODIFICATIONS'
]
=
False
app
.
config
[
'SQLALCHEMY_TRACK_MODIFICATIONS'
]
=
False
db
=
SQLAlchemy
(
app
)
db
.
init_app
(
app
)
# TABELA
# Cria o banco de dados e as colunas (nome, especie, idade, peso)
class
Animal
(
db
.
Model
):
with
app
.
app_context
():
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
db
.
create_all
()
nome
=
db
.
Column
(
db
.
String
(
100
))
tipo
=
db
.
Column
(
db
.
String
(
100
))
idade
=
db
.
Column
(
db
.
Integer
)
# PÁGINA INICIAL
@app.route
(
'/'
)
@app.route
(
'/'
)
def
index
():
def
index
():
animais
=
Animal
.
query
.
all
()
animais
=
Animal
.
query
.
all
()
return
render_template
(
'index.html'
,
animais
=
animais
)
return
render_template
(
'index.html'
,
animais
=
animais
)
# CADASTRAR
@app.route
(
'/cadastro'
)
def
cadastro
():
return
render_template
(
'cadastro.html'
)
@app.route
(
'/adicionar'
,
methods
=
[
'POST'
])
@app.route
(
'/adicionar'
,
methods
=
[
'POST'
])
def
adicionar
():
def
adicionar
():
# O segredo está aqui: os nomes entre [''] devem ser IGUAIS ao 'name' do HTML
nome
=
request
.
form
[
'nome'
]
nome
=
request
.
form
[
'nome'
]
tipo
=
request
.
form
[
'tipo'
]
especie
=
request
.
form
[
'especie'
]
# Antes estava 'tipo', por isso dava erro
idade
=
request
.
form
[
'idade'
]
idade
=
request
.
form
[
'idade'
]
peso
=
request
.
form
[
'peso'
]
# Agora estamos pegando o peso também
novo_animal
=
Animal
(
novo_animal
=
Animal
(
nome
=
nome
,
nome
=
nome
,
tipo
=
tipo
,
especie
=
especie
,
idade
=
idade
idade
=
int
(
idade
),
peso
=
float
(
peso
)
)
)
db
.
session
.
add
(
novo_animal
)
db
.
session
.
add
(
novo_animal
)
db
.
session
.
commit
()
db
.
session
.
commit
()
return
redirect
(
'/'
)
return
redirect
(
url_for
(
'index'
)
)
# EXCLUIR
@app.route
(
'/excluir/<int:id>'
)
@app.route
(
'/excluir/<int:id>'
)
def
excluir
(
id
):
def
excluir
(
id
):
animal
=
Animal
.
query
.
get
(
id
)
animal
=
Animal
.
query
.
get_or_404
(
id
)
db
.
session
.
delete
(
animal
)
if
animal
:
db
.
session
.
commit
()
db
.
session
.
delete
(
animal
)
return
redirect
(
url_for
(
'index'
))
db
.
session
.
commit
()
return
redirect
(
'/'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
with
app
.
app_context
():
db
.
create_all
()
app
.
run
(
debug
=
True
)
app
.
run
(
debug
=
True
)
\ No newline at end of file
fazenda.db
0 → 100644
View file @
7c78a829
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment