我正在构建此应用程序以跟踪事件,并且在编辑页面上我正在尝试update_or_create datetime字段。我刚开始时,它现在非常准确。
虽然我输入了正确的日期时间格式,但它告诉我格式不正确。如何格式化输入字段中的输入以使其被接受为正确的格式?
views.py
def edit(request, event_id):
if request.method == 'POST':
name = request.POST['name']
description = request.POST['description']
location = request.POST['location']
date = str(request.POST['date'])
updated_values = {'name':name, 'location':location, 'date':date, 'description':description}
event = get_object_or_404(Event, pk=event_id)
event = Event.objects.update_or_create(name='name', date='date', defaults=updated_values)
return detail(request, event_id)
else:
event = get_object_or_404(Event, pk=event_id)
return render(request, 'events/edit.html', {'event' : event})
edit.html(表单)
<form method="POST" action="{% url 'events:edit' event.id %}">
{% csrf_token %}
<label>Name</label>
<input type="text" name="name">
<label>Date</label>
<input type="text" name="date">
<label>Location</label>
<input type="text" name="location">
<label>Description</label>
<textarea cols="40" rows="8" name="description"></textarea>
<button type="submit">Submit</button>
</form>
这是错误堆栈
Environment:
Request Method: POST
Request URL: http://0.0.0.0:8000/events/edit/3/
Django Version: 1.9
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
'events.apps.EventsConfig']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Conner/dev/jivetribe/events/views.py" in edit
24. event = Event.objects.update_or_create(name='name', date='date', defaults=updated_values)
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in update_or_create
480. obj = self.get(**lookup)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
378. clone = self.filter(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
790. return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
808. clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
1243. clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
1269. allow_joins=allow_joins, split_subq=split_subq,
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
1203. condition = self.build_lookup(lookups, col, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_lookup
1099. return final_lookup(lhs, rhs)
File "/Library/Python/2.7/site-packages/django/db/models/lookups.py" in __init__
19. self.rhs = self.get_prep_lookup()
File "/Library/Python/2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
57. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
744. return self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
1440. value = super(DateTimeField, self).get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
1296. return self.to_python(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in to_python
1423. params={'value': value},
Exception Type: ValidationError at /events/edit/3/
Exception Value: [u"'date' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
EDIT models.py
from __future__ import unicode_literals
from django.db import models
class Event(models.Model):
name = models.CharField(max_length=250)
description = models.CharField(max_length=500)
location = models.CharField(max_length=500, default = 'null')
date = models.DateTimeField()
def __str__(self):
return str(self.date)