Skip to content
Snippets Groups Projects
Commit 8910cd36 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Envoie du formulaire terminé, TP6 terminé.

parent d74ac5ca
Branches
No related tags found
No related merge requests found
......@@ -8,10 +8,24 @@
{% block content %}
<div class="container">
{% if status %}
{% if status == 'success' %}
<div class="alert-message success" data-alert>
<a class="close" href="#">×</a>
<p><strong>Enregistré !</strong> L'ingrédient {{ nomIngredient }} a bien été ajouté.</p>
</div>
{% endif %}
{% if status == 'error' %}
<div class="alert-message error" data-alert>
<a class="close" href="#">×</a>
<p><strong>Erreur !</strong> L'ingrédient n'a pas pu être ajouté.</p>
</div>
{% endif %}
{% endif %}
<h2>Ajouter un ingrédient</h2>
<p>Renseigner le nom de l'ingrédient à ajouter.</p>
<form action="" method="post" class="form-stacked">
<form action="/ingredients/add/post" method="post" class="form-stacked">
{% csrf_token %}
{% for field in form %}
{% if field.errors %}
......@@ -42,6 +56,7 @@
</div>
{% endblock %}
{% block javascripts %}
<script type="text/javascript" src="{% static 'applipizza/js/bootstrap-alerts.js' %}"></script>
<script type="text/javascript">
var form = $('form');
var formRequiredFields = form.find('input[required]');
......
......@@ -35,3 +35,24 @@ def formulaireCreationIngredient(request):
'applipizza/formulaireCreationIngredient.html',
{"form": formulaire}
)
def creerIngredient(request):
form = IngredientForm(request.POST)
if form.is_valid():
nomIngredient = form.cleaned_data['nom']
ingredient = Ingredient(nom=nomIngredient)
ingredient.save()
formulaire = IngredientForm()
return render(
request,
'applipizza/formulaireCreationIngredient.html',
{"form": IngredientForm(), "status": "success", "nomIngredient": nomIngredient}
)
else:
return render(
request,
'applipizza/formulaireCreationIngredient.html',
{"form": form, "status": "error"}
)
\ No newline at end of file
......@@ -22,5 +22,6 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('pizzas/', views.pizzas),
path('pizza/<int:id>', views.pizza),
path('ingredients/add', views.formulaireCreationIngredient)
path('ingredients/add', views.formulaireCreationIngredient),
path('ingredients/add/post', views.creerIngredient),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment