I want make blog where I have categories and posts inside. Categories should be displayed, and when you click on it, you are redirected to another site where articles of this category are shown.
models.py:
class Category(CMSPlugin):
title = models.CharField(max_length=20, default='category')
def __unicode__(self):
return self.title
class Blog_post(CMSPlugin):
category = models.ForeignKey(Category)
style = models.ForeignKey(Blog_style)
title = models.CharField(max_length=200, default='title')
description = models.CharField(max_length=200,default='description')
image = models.ImageField(upload_to='static', null=True, blank=True)
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 __unicode__(self):
return self.title
views.py
def Blog_list(request):
posts = Blog_post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
category = Category.objects.all()
return render(request, 'blogspot.html', {'posts': posts, 'category':category})
def post_detail(request, pk):
post = get_object_or_404(Blog_post, pk=pk)
return render(request, 'post_detail.html', {'post': post})
def category_detail(request, pk):
cat = get_object_or_404(Category, id=pk)
post_with_category = Blog_post.objects.filter(category=cat)
return render(request, 'articles.html', {'post_with_category': post_with_category})
blogspot.html
{% for post in posts %}
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{post.title}}</a></h1>
<a href="{% url 'category_detail' pk=post.category.id %}" >{{ post.category }}</a>
{{post.title}}
{{ post.description }}
{{ post.image }}
{{ post.text }}{{ post.published_date }}
{% endfor %}
So far works all ok. I can click on {{post.title}} and im redirected to post_detail. Now i want to make same logic with categories. When i click on {{post.category}} i want redirect to articles.html where u can see all articles in specific category.
EDIT:
I inserted code to show posts in categories. I stucked with for loop. If i use loop mentioned in post, I get all posts and categories. The problem is if i have 2 posts in one category and this loop will show 2x "category" in template.
So I edited my for loop.
{% for post in category %}
{{post.title}}
{% endfor %}
If I insert <a href="{% url 'category_detail' pk=post.category.id %}" >{{post.title}} in this loop i get no reverse match.
I tried to modify views.py category_detail
And url should looks like localhost/<category>/
And another question is, what is QRM alternative comand for "select*from Table Where Column_id= id ;
urls.py
url(r'^blog/$', views.Blog_list, name='Blog_list'),
url(r'^blog/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
If I understand your question, django allows you to reference FK objects through the main object. So, since your
post.categoryis an instance of yourCategorymodel, you should be able to usepost.category.idto do a reverse lookup, so your template would have something along the lines of:Then, in your
category_detailview you would just use thepkto get the lookup:Then you could display the list of posts on the new url link that have the same category, via your new
post_with_categoryobject list.EDIT:
Your urls would want to include something like the following for the above html href tag to work: