drjsoundsgood
drjsoundsgood

Reputation: 23

Unable to link Blog Posts to Specific Category Django

I used Tango with Django to create a database containing categories, and I used the Django Girls tutorial to add a blog to the database. Both are working fine, but I have been having trouble linking each blog post to its respective category. Right now, all posts go to my post_list.html page.

If I were doing this from scratch, I would add a new view, add a new template, add a url mapping, and then add a link from the category page. I also am aware that I need to edit my blog post model to contain:

category - models.ForeignKey(Category)

I thought it would be as simple as adding this to my url.py:

url(r'^category/(?P<category_name_slug>[\w\-]+)/post_edit/$', views.add_page, name='add_page'),  

However after a few rounds of trying things along these lines, I can't quite get anything to work. I run into error after error. I have tried going through other blog-related tutorials online, and looking at other stack-overflow posts related to things like this, but I am still stuck. I am a newbie to Django; I am pretty sure that's playing a roll in my lack of success too.

Below is the (unmodified) code I have so far. Can someone help me with this?

My models.py

class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
        default=timezone.now)
published_date = models.DateTimeField(
        blank=True, null=True)

def publish(self):
    self.published_date = timezone.now()
    self.save()

def __str__(self):
    return self.title

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    slug = models.SlugField()

    def save(self, *args, **kwargs):
            # Uncomment if you don't want the slug to change every time the name changes
            #if self.id is None:
                    #self.slug = slugify(self.name)
            self.slug = slugify(self.name)
            super(Category, self).save(*args, **kwargs)

    def __unicode__(self):  #For Python 2, use __str__ on Python 3
        return self.name

My urls.py

    url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
    url(r'^post_list/$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),

My views.py

 def post_list(request):
 posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
 return render(request, 'rango/post_list.html', {'posts': posts})

def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'rango/post_detail.html', {'post': post})

def post_new(request):
if request.method == "POST":
    form = PostForm(request.POST)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = PostForm()
return render(request, 'rango/post_edit.html', {'form': form})

def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
    form = PostForm(request.POST, instance=post)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = PostForm(instance=post)
return render(request, 'rango/post_edit.html', {'form': form})

Thanks in advance.

Upvotes: 1

Views: 904

Answers (1)

drjsoundsgood
drjsoundsgood

Reputation: 23

I realized I had to have my posts filtered to their respective categories. I also switched over to using class based views. The code is a little cleaner.

class PostListView(DetailView):
model = Category
template_name = 'dicer/post_list.html'

def get_context_data(self, **kwargs):
    context = super(PostListView, self).get_context_data(**kwargs)
    posts = (
        Post.objects.filter(category=self.get_object(),
                            published_date__lte=timezone.now())
                    .order_by('published_date')
    )
    context['posts'] = posts
    return context

Upvotes: 1

Related Questions