无法解析关键字' slug'进入田野。选择包括:正文,评论,日期,ID,标题

时间:2018-04-22 15:39:23

标签: python django

嗨我在帖子上写评论有问题请帮帮我。

这是我的网址

    from django.conf.urls import url
    from django.views.generic import ListView, DetailView
    from forum.models import Post
    from . import views
    from .views import createPost

    urlpatterns = [
       url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25], template_name="forum/forum.html")),
       url(r'^(?P<pk>\d+)$', DetailView.as_view(model=Post, template_name='forum/post.html')),
       url(r'^createPost/', createPost.as_view(), name='createPost'),
       url(r'^(?P<slug>[-\w]+)/comment/$', views.add_comment, name='add_comment')
    ]

这是我的观点

    from django.shortcuts import render, redirect
    from .forms import PostForm
    from django.views.generic import TemplateView
    from .forms import CommentForm
    from django.shortcuts import get_object_or_404
    from .models import Post

    class createPost(TemplateView):
        template_name = 'forum/createPost.html'

        def get(self, request):
            form = PostForm()
            return render(request, self.template_name, {'form': form})

        def post(self, request):
            form = PostForm(request.POST)
            if(form.is_valid()):
                form.save()
                return redirect('/forum')

    def add_comment(request, slug):
        post = get_object_or_404(Post, 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('/forum/createPost', slug=post.slug)
        else:
            form = CommentForm()
        template = 'forum/addComment.html'
        context = {'form': form}
        return render(request, template, context)

最后这是我的模特

    from django.db import models

    class Post(models.Model):
        title = models.CharField(max_length=140)
        body = models.CharField(max_length=500)
        date = models.DateTimeField()

        def __str__(self):
            return self.title

    class Comment(models.Model):
        post = models.ForeignKey(Post, related_name='comments', on_delete=None)
        com_title = models.CharField(max_length=140)
        com_body = models.CharField(max_length=500)

        def __str__(self):
            return self.com_title

我认为问题与我使用的模板视图有关,当我调用Post it trys将slug放入模板时。我是Django的新手,我真的很乐意帮助他们。我也有表格文件可能是相关的,所以如果你认为它请现在让我,所以我可以添加到问题。谢谢!

2 个答案:

答案 0 :(得分:2)

比Yasin Bahtiyar更详细一点。

添加到您的Post模型

slug = models.SlugField (
    verbose_name = "Slug",
    allow_unicode = True,
    unique=True)

这将确保slug是独一无二的,你不会得到2条记录,但是你需要确保你的slug在保存之前是唯一的。

如果您想根据标题设置slug,请执行以下操作:

slug = models.SlugField (
    verbose_name = "Slug",
    allow_unicode = True,
    unique=True,
    blank = True,
    null = True)

将一个保存功能添加到模型中:

def save(self, *args, **kwargs):
    if not self.slug: self.slug = slugify(self.title)
    super(Post, self).save(*args, **kwargs)

同样,您需要添加代码以确保它是唯一的。

答案 1 :(得分:0)

What is a "slug" in Django?

class Post(models.Model):
    title = models.CharField(max_length=140)
    body = models.CharField(max_length=500)
    date = models.DateTimeField()
    slug = models.SlugField(max_length=40)