0

HI I have a problem with writing a comment on a post pls help me.

Here is my urls

    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')
    ]

And here is my views

    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)

And lastly here is my models

    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

I think the problem have to do with that I use templateviews and when I call Post it trys to put the slug into the template. I am new to Django and I would really appriciate some help. I also have form file that may be relevante so if you think it is please let me now so I can add it to the question. Thanks!

Geeksquid
  • 31
  • 6

2 Answers2

2

Just a bit more detail than Yasin Bahtiyar.

Add to your Post model

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

This will ensure the slug is unique and you don't get 2 records coming back but you'll need to ensure your slug is unique before saving.

If you want to set your slug based on the title then do something like:

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

AND add a save function to the model:

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

Again, you'll need to add code to ensure it is unique.

HenryM
  • 4,881
  • 4
  • 34
  • 77
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)
Yasin Bahtiyar
  • 2,117
  • 3
  • 17
  • 18