Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crud #43

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

crud #43

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.templates
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FLASK_APP=application.py
FLASK_DEBUG=1
DATABASE_URL=
6 changes: 0 additions & 6 deletions app.py

This file was deleted.

36 changes: 36 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from flask import Flask, request, render_template, redirect, url_for
from dotenv import load_dotenv
from models import db, Estudiantes
load_dotenv()

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'

if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
db.init_app(app)

@app.route('/')
def lista_estudiantes():
estudiantes = Estudiantes.query.all()
return render_template('estudiantes.html', estudiantes=estudiantes)


@app.route('/Crear', methods=['GET', 'POST'])
def nuevo_estudiante():
if request.method == 'POST':
nombre = request.form.get('Nombre')
apellido = request.form.get('Apellido')
ciclo = request.form.get('Ciclo')
programa = request.form.get('Programa')

if nombre and apellido and ciclo and programa:
nuevo_estudiante = Estudiantes(nombre=nombre, apellido=apellido, ciclo=ciclo, programa=programa)
db.session.add(nuevo_estudiante)
db.session.commit()

return redirect(url_for('lista_estudiantes'))

return render_template('index.html')
18 changes: 18 additions & 0 deletions crear_tablas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from models import *
from dotenv import load_dotenv
load_dotenv()

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)

def main():
db.create_all()

if __name__ == "__main__":
with app.app_context():
main()
11 changes: 11 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Estudiantes(db.Model):
__tablename__ = "Estudiantes"
id = db.Column(db.Integer, primary_key=True)
nombre = db.Column(db.String, nullable=False)
apellido = db.Column(db.String, nullable=False)
ciclo = db.Column(db.String, nullable=False)
programa = db.Column(db.String, nullable=False)
Binary file modified requirements.txt
Binary file not shown.
26 changes: 26 additions & 0 deletions templates/estudiantes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<title>Document</title>
<nav aria-label="breadcrumb">
<ul>
<li><a href="">Home</a></li>
<li><a href="/Crear">Category</a></li>
</ul>
</nav>
</head>

<body>
{% for estudiante in estudiantes %}
<article>
<li>Nombre: {{ estudiante.nombre }} {{ estudiante.apellido }} - Ciclo: {{ estudiante.ciclo }} - Programa: {{
estudiante.programa }}</li>
</article>
{% endfor %}
</body>

</html>
37 changes: 37 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<title>Document</title>
<nav aria-label="breadcrumb">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/Crear">Category</a></li>
</ul>
</nav>
</head>

<body>
<article>
<form action="/Crear" method="post">
<label for="Nombre">Nombre</label>
<input type="text" name="Nombre" placeholder="Ingrese el nombre del estudiante">

<label for="Apellido">Apellido</label>
<input type="text" name="Apellido" placeholder="Ingrese el apellido del estudiante">

<label for="Ciclo">Ciclo</label>
<input type="text" name="Ciclo" placeholder="Ingrese el ciclo del estudiante">

<label for="Programa">Programa</label>
<input type="text" name="Programa" placeholder="Ingrese el programa del estudiante">

<input type="submit" placeholder="Crear Estudiante">
</form>
</article>
</body>

</html>