我在views.py中创建了一个评论视图,供用户通过表单编写和提交评论。评论将显示在与显示电影信息相同的模板上。
当我尝试进入其中一部电影时,出现此错误:
/ myapp2 / 2 /处的OperationalError 没有这样的列:myapp2_comentario.pelicula_id
views.py
def detallesPelicula(request, pelicula_id):
peliculas = get_list_or_404(Pelicula.objects.order_by('titulo'))
pelicula = get_object_or_404(Pelicula, pk=pelicula_id)
actor = get_list_or_404(Actor.objects)
comentarios = Comentario.objects.filter(pelicula=pelicula).order_by('fecha')
comment_form = CommentForm(request.POST or None)
if request.method == 'POST':
if comment_form.is_valid():
comentario = comment_form.save(commit=False)
comentario.usuario = request.user
comentario.save()
return HttpResponseRedirect(pelicula.get_absolute_url())
context = {'pelicula': pelicula, 'peliculas': peliculas, 'comentarios':comentarios,'comment_form':comment_form}
return render(request, 'detallesPelicula.html', context)
我的模型。py
class Comentario(models.Model):
usuario = models.ForeignKey(User,on_delete=models.CASCADE)
pelicula =models.ForeignKey(Pelicula, on_delete=models.CASCADE)
fecha = models.DateTimeField(auto_now_add=True,null=True,blank=True)
texto = models.TextField(max_length=2000, default="")
def __str__(self):
return self.usuario.nickname
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comentario
fields = ['texto']