i am building a blog in Django and where register user can write blog articles . I want to display posts that are associated with certain categories on one page. i tried to do that but it does not show the post associated to the article. here is my files
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
class Category(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
def get_absolute_url(self):
# return reverse('article', args=(str(self.id)))
return reverse('home') # go to home page after article submission
class Post(models.Model):
title = models.CharField(max_length=255)
tags = models.CharField(max_length=255, default="Ryan's World")
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default="uncategorized")
def __str__(self):
return self.title+' | '+str(self.author)
def get_absolute_url(self):
# return reverse('article', args=(str(self.id)))
return reverse('home') # go to home page after article submission
views.py created function based view
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post, Category
from .forms import PostForm, EditForm
from django.urls import reverse_lazy
class HomeView(ListView):
model = Post
template_name = 'home.html'
ordering = ['-id']
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats)
return render(request, 'categories.html', {'cats':cats ,'category_posts': category_posts })
. . .
urls.py
from django.urls import path
# from . import views
from .views import HomeView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView, AddCategoryView, CategoryView
urlpatterns = [
# path('', views.home , name='home'),
path('', HomeView.as_view(), name="home"),
path('article/<int:pk>', ArticleDetailView.as_view(), name="article"),
path('add_post/', AddPostView.as_view(), name='add_post'),
path('add_category/', AddCategoryView.as_view(), name='add_category'),
path('article/edit/<int:pk>', UpdatePostView.as_view(), name="updatearticle"),
path('article/<int:pk>/remove', DeletePostView.as_view(), name="deletearticle"),
path('category/<str:cats>/', CategoryView, name='category')
]
forms.py
from django import forms
from .models import Post, Category
choices = Category.objects.all().values_list('name','name')
choice_list = []
for item in choices:
choice_list.append(item)
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'tags', 'author','category' ,'body')
widgets = {
'title': forms.TextInput(attrs={'class':'form-control', 'placeholder': 'this is placeholder'}),
'tags': forms.TextInput(attrs={'class':'form-control'}),
'author': forms.Select(attrs={'class':'form-control'}),
'category': forms.Select( choices = choice_list, attrs={'class':'form-control'}),
'body': forms.Textarea(attrs={'class':'form-control'}),
}