I'm having problem writing a simple form. This is my model for that form.
class UserStock(models.Model):
amount = models.FloatField(default=0)
date = models.DateTimeField('buy date')
stockPrice = models.FloatField(default=0)
user = models.ForeignKey(User)
stock = models.ForeignKey(Stock)
This is my forms.py file
from django import forms
from datetime import date
class AddStockForm(forms.Form):
stock = forms.CharField(max_length=10)
amount = forms.FloatField(initial=0)
#, initial=date.today, input_formats=settings.DATE_INPUT_FORMATS
date = forms.DateField('buy date', initial=date.today)
stockPrice = forms.FloatField(initial=0)
This is my first version for submitting form data.
@login_required(login_url='/login/')
def new_stock(request):
if request.method == 'POST':
form = AddStockForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
#dateForm = datetime.datetime.strptime(cd['date'],"%Y-%m-%d")
userStock = UserStock(amount = cd['amount'], date=cd['date'], stockPrice = cd['StockPrice'])
userStock.user = request.user
stockForm = Stock.objects.filter(symbol=cd['stock'])
userStock.stock = stockForm
userStock.save()
else:
form = AddStockForm()
template = loader.get_template("userStocks/new.html")
context = RequestContext(request, {
'form':form.as_p(),
})
return HttpResponse(template.render(context))
This is what my error looks like. Error while submitting: Enter a valid date. I guess that it's Django's auto-check format. I googled a lot but didn't help. I have tried input_formats variations, but no luck. Thanks in advance for help.