Thursday, December 19, 2019

Re: FieldError at / Django models how to reslove this issue....

You are missing an underscore here.

It should be published_date__lte and not published_date_lte.

Cheers,
Simon

Le jeudi 19 décembre 2019 11:40:05 UTC-5, MEGA NATHAN a écrit :
Hi all.

Cannot resolve keyword 'published_date_lte' into field. Choices are: author, author_id, comments, create_date, id, published_date, text, title
  

this are models:
from django.db import models
from django.utils import timezone
from django.urls import reverse

# Create your models here.

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE,)
    title = models.CharField(max_length=200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now())
    published_date = models.DateTimeField(blank=Truenull=True

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    #def approve_comments(self):
        #return self.comments.filter(approved_comments=True)
    
    def approved_comments(self):
        return self.comments.filter(approved_comment=True)

    #def get_absolute_url(self):
        #return reverse("post_detail", kwargs={"pk": self.pk})

    def __str__self():
        return self.title

class Comment(models.Model):
    post = models.ForeignKey('blog.post',related_name='comments', on_delete=models.CASCADE,)
    title = models.CharField(max_length=200)
    text = models.TextField()
    create_date = models.DateTimeField(default=timezone.now())
    published_date = models.DateTimeField(blank=Truenull=True
    approved_comment = models.BooleanField(default=False)

    def approve(self):
        self.approved_comment True
        self.save()
    
    def get_absolute_url(self):
        return reverse('post_list')
 
    def __str__(self):
        return self.text

And views:

from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
#from django.utils.timezone.now`
from blog.models import Post, Comment
from .forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin 
from django.views.generic import (TemplateView, ListView, DetailView, CreateView,
                                UpdateView, DeleteView)
                            
# Create your views here.

class Aboutview(TemplateView):
    template_name = 'about.html'

class PostListView(ListView):
    model = Post

    def get_queryset(self):
        return Post.objects.filter(published_date_lte=timezone.now()).order_by('-published_date')

class PostDetailView(DetailView):
    model = Post

class CreatePostView(LoginRequiredMixin,CreateView):
    Login_url = '/Login/'
    redirect_field_name = 'blog/post_detail.html'
    from_class = PostForm
    model = Post

class PostUpdateView(LoginRequiredMixin, UpdateView):
    Login_url = '/Login/'
    redirect_field_name = 'blog/post_detail.html'
    from_class = PostForm
    model = Post
   
#class PostDeleteView(loginrequiredMixin, DeleteView):
class PostDeleteView(LoginRequiredMixin,DeleteView):
    model = Post
    success_url = reverse_lazy('post_list')

class DraftListView(LoginRequiredMixin, ListView):
    login_url = '/Login/'
    redirect_field_name = 'blog/post_list.html'
    model = Post

    def get__queryset(self):
        return Post.objects.filter(published_date_isnull=True).order_by('created_date')


#############################
##############################
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/comment_form.html', {'form': form})

@login_required
def post_publish(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.publish
    return redirect('blog/post_detail.html', pk=pk)

#@login_required
def comment_approve(request,pk):
    comment = get_object_or_404(comment,pk=pk)
    comment.approve()
    return redirect('post_detail',pk=comment.post.pk)

#@login_required
def comment_remove(request,pk):
    #comment = get_object_or_404(comment, pk=pk)
    #comment.remove()
    #return redirect('post_detail',pk=comment.post.pk)
    comment = get_object_or_404(comment, pk=pk)
    post_pk = comment.post.pk
    comment_delete()
    return redirect('post_detail',pk=comment.post.pk)

blog urls:

#from django.conf.urls import path
from django.urls import include, path

from blog import views

urlpatterns = [
    path('',views.PostListView.as_view(), name='Post_list'),
    path('about', views.Aboutview.as_view, name='about'),
    path('Post/(?P<pk>\d+)'views.PostDetailView.as_view(), name='post_detail'),
    path('Post/new/', views.CreatePostView.as_view(), name='post_new'),
    path('Post/(?P<pk>\d+)/edit/', views.PostUpdateView.as_view(), name='post_edit'),
    path('Post/(?P<pk>\d+)/remove/', views.PostDeleteView.as_view(), name='post_remove'),
    path('drafts/', views.DraftListView.as_view(), name='post_draft_list'),
    #path('Post/(?P<pk>\d+)/comment/',views.add_comment_to_post()error, name='add_comment_to_post'),
    path('Post/(?P<pk>\d+)/comment/',views.add_comment_to_post, name='add_comment_to_post'),
    path('comment/(?P<pk>\d+)/approve/',views.comment_approve,name='comment_approve'),
    path('comment/(?P<pk>\d+)/remove/',views.comment_remove,name='comment_remove'),
    path('Post/(?P<pk>\d+)/publish/',views.post_publish, name='post_publish'),
]

site url:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
    path('accounts/login/',auth_views.LoginView.as_view(template_name='blog/login.html')),
    path('accounts/logout/',views.logout, name='logout',kwargs={'next_page''/'}),
]



How to reslove this field error:

        
                


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/08676c08-583e-48b2-ad49-cb18fd5c86cb%40googlegroups.com.

No comments:

Post a Comment