django:没有Techpost匹配给定的查询

时间:2018-05-28 19:38:41

标签: python django

我是新的Django并且一直从事项目工作。当我点击我的techposts。它说No Techpost matches the given query。它工作正常,我没有改变任何东西。不确定会发生什么。它发生了2-3次。数据库刷新用于解决此问题。但现在没有任何工作。请建议我如何解决这个问题及其背后可能的原因?

PS:发现了类似的问题,但他们的答案没有帮助。

CODE:

models.py

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager, self).get_queryset().filter(status='published')


class Techpost(models.Model):
    STATUS_CHOICS=(
        ('draft', 'Draft'),
        ('published', 'Published')
    )

    title = models.CharField(max_length=255, blank=False, null=False)
    slug = models.SlugField(max_length=255, unique_for_date='publish')
    author = models.ForeignKey(User, on_delete=models.CASCADE, 
    related_name='tech_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices = STATUS_CHOICS, default='draft')
    tags = TaggableManager()


class Meta:
    ordering = ('-publish',)

def __str__(self):
    return self.title


objects = models.Manager() # default manager
published = PublishedManager() # Custom Manager.


def get_absolute_url(self):
    return reverse('rincon:techpost_details', args=[
        self.publish.year,
        self.publish.strftime('%m'),
        self.publish.strftime('%d'),
        self.slug
    ])


class Comment(models.Model):
    techpost = models.ForeignKey(Techpost, on_delete=models.CASCADE, 
    related_name='comments')
    comment     = models.TextField()
    created  = models.DateTimeField(auto_now_add=True)
    updated  = models.DateTimeField(auto_now =True)
    active   = models.BooleanField(default=True)

class Meta:
    ordering = ('created',)

def __str__(self):
    return "Comment on {}".format(self.techpost)

views.py

@login_required
def techpost_details(request, year, month, day, techpost):

techpost = get_object_or_404(Techpost, slug=techpost, status='published',
                             publish__year=year, publish__month=month, publish__day=day)
comments = techpost.comments.filter(active=True)
if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        new_comment = comment_form.save(commit=False)
        new_comment.techpost = techpost
        new_comment.save()
        comment_form = CommentForm()
else:
    comment_form = CommentForm()

techpost_tags_ids = techpost.tags.values_list('id', flat=True)
similar_techposts = Techpost.published.filter(tags__in=techpost_tags_ids)\
    .exclude(id=techpost.id)
similar_techposts = similar_techposts.annotate(same_tags=Count('tags'))\
    .order_by('-same_tags', '-publish')[:4]
return render(request, 'rincon/details.html', {'techpost': techpost, 'comments': comments, 'comment_form': comment_form, 'similar_techposts': similar_techposts})

urls.py

app_name = 'rincon'
urlpatterns =[
path('', views.home, name='home'),
path('submit/', views.request_submit, name='request_submit'),
path('techposts/', views.techpost_list, name='techpost_list'),
path('tag/<slug:tag_slug>/', views.techpost_list, name='techpost_list_by_tag'),
path('<slug:techpost_id>/share/', views.techpost_share, name='techpost_share'),
path('search/', views.techpost_search, name='techpost_search'),
path('hotfixes/', views.hotfixes, name='hotfixes'),
path('autocomplete/', views.autocomplete, name='autocomplete'),
path('<int:year>/<int:month>/<int:day>/<slug:techpost>/', views.techpost_details, name='techpost_details'),

0 个答案:

没有答案