I have a form the user selects a date from a jQuery datepicker which is submitted to create a Proposal object. This error has never happened to me in testing, but has happened to users on my live server (part of my problem debugging it). The oddest part is that the object is created in the database even though the error is on the line where the object is created. Any ideas?
Date is stored like this in the Db: 2011-08-17
Error: ValidationError: [u'Enter a valid date in YYYY-MM-DD format.']
src/views.py", line 1901, in new_proposal proposal = Proposal.objects.create(user=request.user, host=host, date=date)
HTML:
<form action="{% url new_proposal %}" method="post" accept-charset="utf-8" style="padding:0">{% csrf_token %}
<span>Date:</span>
<input type="date" name="date" id="datepicker" placeholder="yyyy/mm/dd"/>
<input type="hidden" name="host" id="{{profile.user.id}}" value="{{profile.user.id}}">
<input type="submit" value="Book it!" class="action">
</form>
View:
@login_required
def new_proposal(request):
host_id = request.POST.get('host')
host = User.objects.get(id=host_id)
date = request.POST.get('date')
price = host.get_profile().price
proposal = Proposal.objects.create(user=request.user, host=host, date=date)
proposal.price = actual_price
proposal.save()
In the Django error email it seems like the Date isn't in the POST but it is when I call request.POST.get('date').
POST:<开发者_JAVA技巧;QueryDict: {u'date': [u''], u'csrfmidd
I believe this is the solution. Use a ModelForm + DateField for the Proposal object. Then in the view:
if request.POST:
if form.is_valid():
As I haven't personally experienced this error, I am still not sure - but my view & form are now in proper Django format.
精彩评论