simple form contact
This commit is contained in:
parent
96083a40a0
commit
994374ca33
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
instance/
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
25
app.py
Normal file
25
app.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from flask import Flask, render_template, redirect, url_for, flash
|
||||||
|
from forms import ContactForm
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['SECRET_KEY'] = 'votre_cle_secrete_pour_flask_wtf'
|
||||||
|
|
||||||
|
@app.route('/contact', methods=['GET', 'POST'])
|
||||||
|
def contact():
|
||||||
|
form = ContactForm()
|
||||||
|
# Si on arrive sur la page après validation du formulaire (POST)
|
||||||
|
if form.validate_on_submit():
|
||||||
|
# Traitement les données du formulaire
|
||||||
|
name = form.name.data
|
||||||
|
email = form.email.data
|
||||||
|
message = form.message.data
|
||||||
|
|
||||||
|
# Par exemple, enregistrer ou envoyer un email
|
||||||
|
flash(f"Merci {name}, voici le message: {message}", "success")
|
||||||
|
return redirect(url_for('contact'))
|
||||||
|
|
||||||
|
# Si on arrive sur la page sans validation du formulaire (GET)
|
||||||
|
return render_template('form.html', form=form)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(debug=True)
|
||||||
17
config.py
Normal file
17
config.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
"""Configuration de base."""
|
||||||
|
# Clé secrète pour la protection des sessions, cookies et formulaires
|
||||||
|
SECRET_KEY = os.environ.get('SECRET_KEY') or 'votre_cle_secrete'
|
||||||
|
|
||||||
|
# Configuration de la base de données
|
||||||
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///app.db'
|
||||||
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
||||||
|
# Options pour le débogage
|
||||||
|
DEBUG = False
|
||||||
|
|
||||||
|
# Autres configurations
|
||||||
|
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER') or 'uploads'
|
||||||
|
MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # Taille maximale d'un fichier téléchargé (ici: 16 Mo)
|
||||||
18
forms.py
Normal file
18
forms.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from flask_wtf import FlaskForm
|
||||||
|
from wtforms import StringField, TextAreaField, SubmitField
|
||||||
|
from wtforms.validators import DataRequired, Email, Length
|
||||||
|
|
||||||
|
class ContactForm(FlaskForm):
|
||||||
|
name = StringField(
|
||||||
|
'Nom',
|
||||||
|
validators=[DataRequired(), Length(min=2, max=50)]
|
||||||
|
)
|
||||||
|
email = StringField(
|
||||||
|
'Email',
|
||||||
|
validators=[DataRequired(), Email()]
|
||||||
|
)
|
||||||
|
message = TextAreaField(
|
||||||
|
'Message',
|
||||||
|
validators=[DataRequired(), Length(min=10, max=500)]
|
||||||
|
)
|
||||||
|
submit = SubmitField('Envoyer')
|
||||||
18
requirements.txt
Normal file
18
requirements.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
blinker==1.9.0
|
||||||
|
click==8.1.8
|
||||||
|
dnspython==2.7.0
|
||||||
|
email_validator==2.2.0
|
||||||
|
Flask==3.1.0
|
||||||
|
Flask-SQLAlchemy==3.1.1
|
||||||
|
Flask-WTF==1.2.2
|
||||||
|
greenlet==3.1.1
|
||||||
|
gunicorn==23.0.0
|
||||||
|
idna==3.10
|
||||||
|
itsdangerous==2.2.0
|
||||||
|
Jinja2==3.1.5
|
||||||
|
MarkupSafe==3.0.2
|
||||||
|
packaging==24.2
|
||||||
|
SQLAlchemy==2.0.37
|
||||||
|
typing_extensions==4.12.2
|
||||||
|
Werkzeug==3.1.3
|
||||||
|
WTForms==3.2.1
|
||||||
6
static/styles.css
Normal file
6
static/styles.css
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: red;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
24
templates/base.html
Normal file
24
templates/base.html
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fr">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{% block title %}Mon Site{% endblock %}</title>
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<header>
|
||||||
|
<h1>Bienvenue sur mon site</h1>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
{% block content %}{% endblock %}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer>
|
||||||
|
<p>© 2025 Mon Site. Tous droits réservés.</p>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
49
templates/form.html
Normal file
49
templates/form.html
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Contact</title>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container mt-5">
|
||||||
|
<h1>Contactez-nous</h1>
|
||||||
|
<form method="POST">
|
||||||
|
<!-- CSRF token (voir plus bas)-->
|
||||||
|
{{ form.hidden_tag() }}
|
||||||
|
<div class="mb-3">
|
||||||
|
{{ form.name.label(class="form-label") }}
|
||||||
|
{{ form.name(class="form-control", placeholder="nom") }}
|
||||||
|
{% for error in form.name.errors %}
|
||||||
|
<div class="text-danger">{{ error }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
{{ form.email.label(class="form-label") }}
|
||||||
|
{{ form.email(class="form-control", placeholder="email") }}
|
||||||
|
{% for error in form.email.errors %}
|
||||||
|
<div class="text-danger">{{ error }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
{{ form.message.label(class="form-label") }}
|
||||||
|
{{ form.message(class="form-control", placeholder="message") }}
|
||||||
|
{% for error in form.message.errors %}
|
||||||
|
<div class="text-danger">{{ error }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">{{ form.submit.label }}</button>
|
||||||
|
</form>
|
||||||
|
{% with messages = get_flashed_messages(with_categories=True) %}
|
||||||
|
{% if messages %}
|
||||||
|
<div class="mt-3">
|
||||||
|
{% for category, message in messages %}
|
||||||
|
<div class="alert alert-{{ category }}">{{ message }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endwith %}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
24
templates/home.html
Normal file
24
templates/home.html
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{ title }}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
<p>Bonjour, {{ user.username }} !</p>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<h2>Messages :</h2>
|
||||||
|
<ul>
|
||||||
|
{% for post in posts %}
|
||||||
|
<li>
|
||||||
|
<strong>{{ post.author }}</strong>: {{ post.content }}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
27
templates/users.html
Normal file
27
templates/users.html
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Liste des utilisateurs</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Liste des utilisateurs</h1>
|
||||||
|
<table border="1">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Nom d'utilisateur</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for user in users %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ user.id }}</td>
|
||||||
|
<td>{{ user.username }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user