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

2024-10-02 00:38:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的Django,一直在做这个项目。当我点击我的techposts时。上面写着No Techpost matches the given query。它运行得很好,我没有改变任何事情。不知道会发生什么。已经发生过2-3次了。用于修复此问题的数据库刷新。但现在什么都没用了。请建议我如何解决这个问题,以及背后可能的原因?你知道吗

附言:发现类似的问题,但他们的答案没有帮助。你知道吗

代码:

你知道吗型号.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)

你知道吗视图.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})

你知道吗网址.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'),

Tags: pathnameselfformtruemodelsrequesttags

热门问题