From the polls app, I understand how to add multiple items at once this in the admin panel for input, but I am struggling to find a way to do this through CreateView.
Basically, I want the user to be able to use the addingredients page to add more than one ingredient at once the same way it functions on the admin page of the polls app with adding additional choices.
Should I be looking at building a form for input to add this type of functionality? I’m new to Django and fairly new to programming in general, so I would appreciate any help on this, thanks!
Here is my view:
class addingredients(CreateView):
model = Ingredient
template_name = 'recipeBook/addingredients.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}
def get_success_url(self):
return reverse('recipeBook:recipe_details', kwargs={'pk': self.object.recipe.pk})
Here is my model:
class Ingredient(models.Model):
UNIT_CHOICES = [
('TB', 'tablespoons'),
('TS', 'teaspoons'),
('OZ', 'ounces'),
('FO', 'fluid ounces'),
('CP', 'cups'),
('QT', 'quarts'),
('PT', 'pints'),
('GL', 'gallons'),
('LB', 'pounds'),
('ML', 'milliliters'),
('GR', 'grams'),
('KG', 'kilograms'),
('LI', 'liters'),
('UN', 'units')
]
name = models.CharField(max_length=50)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
quantity = models.FloatField(default=0, blank=False)
units = models.CharField(max_length=2, choices=UNIT_CHOICES, default='UN')
def __str__(self):
return self.name
Here is my Template:
{% extends 'recipeBook/base.html' %}
{% block title %}Add Ingredients{% endblock title %}
{% block content %}
<h1>Step 1: Recipe Details</h1>
<form action="" method ="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Add">
</form>
{% endblock content %}