Hello. I am quite new to Django and I am working on this project. I am trying to save a recipe into the database and after correcting a few errors I get the “Success” message. However, when I open the database with the sqlite viewer it shows that the table is empty.
Here is my views file:
from django.http import HttpResponse, HttpResponseRedirect
import json
from django.http import StreamingHttpResponse
from django.shortcuts import render
from django.core.serializers import serialize
from django.http import JsonResponse
from django.template import RequestContext
from .models import StockYeast, queryset_to_indexlist,queryset_to_array, Brew, Fermentable, Miscs, RecipeYeast, Recipes, Yeast,StockFermentables
from .models import Hop,StockHop,StockMiscs
from .forms import RecipeForm, RecipeFermentableForm, RecipeHopForm, RecipeMiscForm,RecipeWaterForm, RecipeYeastForm,RecipeMashForm, StockMiscForm
from .forms import StockFermentableForm,StockHopForm,StockYeastForm, StockMiscForm
from django.forms import formset_factory
def index(request):
return render(request, 'index.html')
def recipes(request):
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
n = form.cleaned_data["name"]
t = Recipes(name=n)
t.save()
return HttpResponse("Success")
else:
recipes = Recipes.objects.all()
recipeFermentableFormset = formset_factory(RecipeFermentableForm, extra=1)
recipeHopFormset = formset_factory(RecipeHopForm, extra=1)
RecipeMiscFormSet= formset_factory(RecipeMiscForm, extra=1)
RecipeYeastFormSet = formset_factory(RecipeYeastForm,extra=1)
RecipeMashFormSet = formset_factory(RecipeMashForm,extra=1)
return render(request, 'recipes.html', {'recipes':recipes,"recipe_form" :RecipeForm(auto_id="recipe_%s"),
"recipe_fermentableformset":recipeFermentableFormset(prefix='fermentable'),
"recipe_hopformset":recipeHopFormset(prefix='hop'),
"recipe_miscformset":RecipeMiscFormSet(prefix='misc'),
"recipe_yeastformset":RecipeYeastFormSet(prefix='yeast'),
"recipe_mashformset" :RecipeMashFormSet(prefix='mash'),
"recipeWaterForm":RecipeWaterForm()})
Here is my recipes.html:
{% block content %}
<!-- Modal -->
<div class="card shadow-sm">
<div class="card-header" id="headingOne">
<h5 class="mb-0"> Receptas</h5>
</div>
<div id="card-base" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<div class="row">
<div class="col">
<form method="post" action="/recipes">
{% csrf_token %}
{{recipe_form.ibu}}
{{recipe_form.abv}}
<div class="row">
<div class="col">{{recipe_form.name|as_crispy_field}}</div>
<div class="col">{{recipe_form.style|as_crispy_field}}</div>
</div>
<div class="row">
<div class="col">{{recipe_form.batch_size|as_crispy_field}}</div>
<div class="col">{{recipe_form.boile_time|as_crispy_field}}</div>
<div class="col">{{recipe_form.efficiency|as_crispy_field}}</div>
</div>
<div class="row">
<div class="col">{{recipe_form.primary_age|as_crispy_field}}</div>
<div class="col">{{recipe_form.secondary_age|as_crispy_field}}</div>
<div class="col">{{recipe_form.age|as_crispy_field}}</div>
</div>
<div class="row">
<div class="col">{{recipe_form.pub_date|as_crispy_field}}</div>
</div>
<div class="row">
<div class="col">{{recipe_form.notes|as_crispy_field}}</div>
</div>
</div>
<span class="d-flex flex-row-reverse"><button class="btn btn-secondary" id="save_recipe_btn" type="submit"><i class="bi bi-save"></i></br> Saugoti</button></span>
</form>
This is going to be a many to one relationship with recipes and recipefermentables, recipehops and so on. However, for starters I am trying to save just the basic portion of recipe.
Is there something wrong with the code or what could be the reason why the data is not being saved?