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, titlethis are models:from django.db import modelsfrom django.utils importtimezone from django.urls importreverse # 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=True, null=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.titleclass 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=True, null=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.textAnd views:from django.shortcuts importrender, get_object_or_404, redirect from django.utils importtimezone #from django.utils.timezone.now` from blog.models import Post,Comment from .forms import PostForm,CommentForm from django.urls importreverse_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 = Postdef get_queryset(self):return Post.objects.filter(published_date_lte= timezone.now()).order_by('- published_date' )class PostDetailView(DetailView): model = Postclass CreatePostView(LoginRequiredMixin,CreateView) : Login_url = '/Login/'redirect_field_name = 'blog/post_detail.html' from_class = PostFormmodel = Postclass PostUpdateView(LoginRequiredMixin, UpdateView): Login_url = '/Login/'redirect_field_name = 'blog/post_detail.html' from_class = PostFormmodel = Post#class PostDeleteView(loginrequiredMixin, DeleteView): class PostDeleteView(LoginRequiredMixin,DeleteView) : model = Postsuccess_url = reverse_lazy('post_list') class DraftListView(LoginRequiredMixin, ListView): login_url = '/Login/'redirect_field_name = 'blog/post_list.html' model = Postdef get__queryset(self):return Post.objects.filter(published_date_isnull=T rue ).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()else:form = CommentForm()return render(request, 'blog/comment_form.html' , {'form' : form})@login_requireddef post_publish(request, pk):post = get_object_or_404(Post, pk=pk) post.publishreturn redirect('blog/post_detail.html' , pk=pk)#@login_requireddef comment_approve(request,pk): comment = get_object_or_404(comment,pk=pk) comment.approve()#@login_requireddef 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.pkcomment_delete()blog urls:#from django.conf.urls importpath from django.urls importinclude, path from blog import viewsurlpatterns = [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