So I have the following model :
class Recipe(models.Model):
title = models.CharField(max_length=100)
ingredients = models.TextField(max_length=200,help_text="Put the ingredients required for the recepies here !")
instructions = models.TextField(max_length=500)
posted_on = models.DateTimeField('Posted On')
def __unicode__(self):
return self.title
Now what I want to do is that I have a front-end in html called add.html which has a form like:
<!DOCTYPE html>
<head><title>New Recipe</title></head>
<body>
<h1>Add A new Recipe Here</h1>
<form action="/recipes/add/" method="post">
{% csrf_token %}
<label>ID<label>
<input type="number" name="id"></input><br />
<label>Title </label>
<input type ="text" name="title"><br />
<label>Ingredients</label>
<input type="text" name="ingredients" />
<br />
<label>Instructions </label>
<input type="text" name="instructions" />
...
Here is how I am saving the form using ModelForm:
def add(request):
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
form.save()
#redirect
return HttpResponse("Thank you")
else:
return HttpResponse("Form Not Valid")
else:
form = RecipeForm()
context = Context({'form':form,})
context.update(csrf(request))
template = loader.get_template('myApp/add.html')
return HttpResponse(template.render(context))
When I run this I always get "form Invalid"
So now my problem is, Should the html form add.html have the EXACT mappings as my model Recipe ?
If yes ,then
- How do I add the corresponding
typesin the html form (forposted_on) ? - How do I handle the
idthat is implicitly created bysyncdb? - Is there any alternative ?
I have just started to learn Django