反向使用关键字参数'{'kwargs':{'slug':'long-establis'的'post-detail'

时间:2019-03-17 05:08:43

标签: python django

我正在尝试在我的详细信息后视图中添加评论字段。但是我在调​​用save()之后立即添加了重定向URL。这给我这样的错误。

  

未找到带有关键字参数'{'kwargs':{'slug':'reader-fact-that-a-reader-will'}}的'post-detail'。尝试了1种模式:['post /(?P [^ /] +)/ $']

这是我的代码

  

posts / views.py

@login_required
def postDetail(request, slug):
    post = get_object_or_404(Post, slug=slug)
    latest_post = Post.objects.order_by('-timestamp')[0:4]
    form = CommentForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.instance.user = request.user
            form.instance.post = post
            form.save()
            return redirect('post-detail', kwargs = {
                'slug': post.slug
            })
    context ={
        'form': form,
        'post': post,
        'latest_post': latest_post
    }
    return render(request, 'posts/post_detail.html', context)
  

posts / urls.py

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

from posts.views import index,postDetail, categoryDetail, blog, search

urlpatterns = [
    path('', index, name="home"),
    path('blog/', blog, name="blog"),
    path('search/', search, name='search'),
    path('post/<str:slug>/', postDetail, name='post-detail'),
    path('category/<slug>/', categoryDetail, name='category-detail'),    
]
  

posts / models.py

from tinymce import HTMLField
from django.db import models
from django.contrib.auth import get_user_model
from slugger import AutoSlugField
from django.urls import reverse
# Create your models here.
User = get_user_model()


def upload_location(instance, filename):
    return "%s/%s" %(instance.slug, filename)


class Author(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.user.username

class Category(models.Model):
    title = models.CharField(max_length=20)
    slug = AutoSlugField(populate_from='title')
    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('category-detail', kwargs={'slug': self.slug})


class Post(models.Model):
    title = models.CharField(max_length = 100)
    slug = AutoSlugField(populate_from='title')
    overview = models.CharField(max_length= 200)
    timestamp = models.DateTimeField(auto_now_add=True)
    content = HTMLField()
    comment_count = models.IntegerField(default=0)
    view_count = models.IntegerField(default=0)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    thumbnail = models.ImageField(
        upload_to=upload_location,
        null=True, 
        blank=True)
    category = models.ManyToManyField(Category)
    featured = models.BooleanField()
    previous_post = models.ForeignKey('self', related_name= 'previous', on_delete=models.SET_NULL, blank=True, null=True)
    next_post = models.ForeignKey('self', related_name= 'next', on_delete=models.SET_NULL, blank=True, null=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'slug': self.slug})

    @property
    def get_comments(self):
        return self.comments.all().order_by('-timestamp')

class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True)
    content = models.TextField()
    post = models.ForeignKey(Post, related_name='comments',on_delete=models.CASCADE)

    def __str__(self):
        return self.user.username

1 个答案:

答案 0 :(得分:1)

在第4条路径中,您使用了target-我不认为URL捕获类型<str:slug>会捕获在段中使用的破折号。

请尝试以下路径:str

编辑

您在path('post/<slug:slug>/', postDetail, name='post-detail'),重定向中也缺少reverse

postDetail()
  

https://docs.djangoproject.com/en/2.1/topics/http/urls/#path-converters