首页 > 解决方案 > Post matching query does not exist. - Django error while trying to access URL of other pages

问题描述

Everything was okay till I created a blog app in my Django projects, now when I try to access the other apps(pages) on the website, it gives me this error. I can't access the URLs under the other apps, I can only access the URLs under the blog

blog/views.py

from django.shortcuts import render, redirect
from .models import Post
from .forms import commentform

def blog(request):
    posts = Post.objects.all(pk=id)
    return render(request, 'blog.html', {'posts': posts})

def howtouse(request):
    return render(request, 'howtouse.html')

def full_post(request, slug):
    post = Post.objects.get(slug=slug)
    if request.method == 'POST':
        form = commentform(request.POST)

        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()

            return redirect('full_post', slug=post.slug)
    else:
        form = commentform()
    return render(request, 'full_post.html', {'post': post, 'form': form})

blog/models.py

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    intro = models.TextField()
    body = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-date_added']


class Comments(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    email = models.EmailField()
    body = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['date_added']



标签: pythondjango

解决方案


推荐阅读