Esempio n. 1
0
def showComments(request,slug):
    #obtenemos el foro asociado a un tema
    #Hemos cambiado los campos tema por slug, que es el tema en forma slugify, ej: tema-de-prueba
    form = Forum.objects.get(slug=slug)
    #Obtenemos el tema asociado al foro, filtrado por slug
    theme = form.theme
    #filtramos comentarios de un foro con un tema, ordenados por la fecha más reciente
    comForm = Comment.objects.filter(theme=form).order_by('-date')

    if request.method =="POST":
        idC = 0
        com = Comment.objects.order_by('-idComment')[:1]
        for c in com:
            idC = c.idComment+1

        com = Comments(request.POST)

        username = request.user
        f = get_object_or_404(Forum,slug=slug)
        #validamos el formulario
        if com.is_valid():
            comment = Comment()
            comment.theme = f
            comment.idComment = idC
            comment.username = username
            comment.commentText = request.POST["commentText"]
            comment.title = request.POST["title"]
            comment.date = datetime.date.today()

            comment.save()

            return redirect("/foros/theme/"+slug,{'com': comForm,})
    else:
        com = Comment.objects.order_by('-idComment')[:1]
        for c in com:
            idC = c.idComment+1
        com = Comment()

    context = {'com': comForm,'commentForm':com,'theme': theme}
    return render(request,'comentarios.html',context)
Esempio n. 2
0
def comment(request,theme):
    if request.method =="POST":
        idC = 0
        com = Comment.objects.order_by('-idComment')[:1]
        for c in com:
            idC = c.idComment+1


        com = Comments(request.POST)

        username = request.user
        f = get_object_or_404(Forum,theme=theme)
        #validamos el formulario
        if com.is_valid():
            comment = Comment()
            comment.theme = f
            comment.idComment = idC
            comment.username = username
            comment.commentText = request.POST["commentText"]
            comment.title = request.POST["title"]
            comment.date = datetime.date.today()
            comment.save()

            return (showComments(request,theme))
        else:
            print comm.errors
    else:
        com = Comment.objects.order_by('-idComment')[:1]
        for c in com:
            idC = c.idComment+1
        com = Comment()

    return render(request,'comentarios.html',{'commentForm':com})