Django:使用DateTimeField的objects.all()错误

时间:2015-07-01 11:39:22

标签: python django

我有一个表单将帖子发送到我的数据库,它运行良好,问题是当我在views.py中执行这样的操作时:

query = Post.objects.filter(pseudonimo = request.session['member_id'])
query = Post.objects.all()

当我转到使用它的页面时,我有这个错误:

Request Method: GET
Request URL:    http://localhost:8000/home/
Django Version: 1.8
Exception Type: TypeError
Exception Value: expected string or buffer
Exception Location: /home/rebecca/DSI/env-pretec/local/lib/python2.7/site-packages/django/utils/dateparse.py in parse_datetime, line 93
Python Executable:  /home/rebecca/DSI/env-pretec/bin/python
Python Version: 2.7.3

如果我删除了查询,它就可以了。

这是我的forms.py:

class PostForm(forms.Form):
titulo = forms.CharField(label='Titulo', max_length=50, min_length=2)
texto = forms.CharField(label='Texto', max_length=500, min_length=5,    widget=forms.Textarea)
fecha = forms.DateTimeField(initial=datetime.datetime.now().strftime("%m/%d/%Y %H:%M:%S"))

这是我的models.py:

class Post(models.Model):
pseudonimo = models.ForeignKey('usuarios.Usuario')
titulo = models.CharField(max_length=30)
texto = models.CharField(max_length=240)
fecha = models.DateTimeField(null=True)

和我的views.py

ef set_post(request):
if request.method == 'POST':
    form=PostForm(request.POST)
    if form.is_valid():
        #variable = formulario
        titulo = form.cleaned_data['titulo']
        texto = form.cleaned_data['texto']
        date  = form.cleaned_data['fecha']
        usuario = Usuario.objects.get(pseudonimo = request.session['member_id'])
        #entrada en la base de datos
        post = Post.objects.create(
                        pseudonimo = usuario,
                        titulo = titulo,
                        texto = texto,
                        fecha = date,)
        post.save()
        return render(request, 'micropost_enviado.html')
else:
    form = PostForm()
return render(request, 'formulario_microposts.html', {'pseudonimo':request.session['member_id'],'form' : form})

如果我使用DateField完成所有这些工作,但我想使用DateTimeField。 我做错了什么?在此先感谢

0 个答案:

没有答案