无法将关键字“ created”创建为字段。选项包括:日期,ID,帖子,用户,user_id?

时间:2018-10-12 15:09:16

标签: python django

嗨,我正在尝试向模型中添加一个名为“ created”的字段,但是出现以下错误: 无法将关键字“ created”创建为字段。选项包括:日期,id,帖子,用户,user_id

跟踪:

FieldError at /home/
Cannot resolve keyword 'created' into field. Choices are: date, id, post, user, user_id

Error during template rendering
In template C:\Users\josep\beginnerProjects\tutorial\accounts\templates\base.html, error at line 0

cmd /终端:

"Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'created' into field. Choices are: date, id, post, user, user_id

models.py:

class Post(models.Model):
    post = models.CharField(max_length=500)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

views.py:

class HomeView(TemplateView):
   template_name = ('home/home.html')

   def get(self, request):
      form = HomeForm()
      posts = Post.objects.all().order_by('created')

      args = {'form': form, 'posts': posts}
      return render(request, self.template_name, args)

   def post(self, request):
      form = HomeForm(request.POST)
      if form.is_valid():
        post = form.save(commit=False)
        post.user = request.user
        post.save()
        text = form.cleaned_data['post']
        form = HomeForm()
        return redirect('home:home')

      args = {'form': form, 'text': text}
      return render(request, self.template_name, args)

0 个答案:

没有答案