I am trying to create a blog, and want that articles were devided by categories. Version of Django:2.1, Python:3.7
I don't understand how should I change the path in the urlpatterns for displaying the name of category, not <int:pk>
. Where can I check examples of the Django code?
I tried to do path('category.title') but it doesn't work.
This is my models.py:
from django.db import models
from django.conf import settings
from django.urls import reverse
class Category(models.Model):
title = models.CharField(max_length=50)
image = models.ImageField(upload_to='', blank=True)
def __str__(self):
return self.title
class Article(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
image = models.ImageField(upload_to='', blank=True)
cat = models.ForeignKey(
Category, on_delete=models.CASCADE, null=True,
)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('article_detail', args=[str(self.id)])
class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='comments')
comment = models.CharField(max_length=100)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
def __str__(self):
return self.comment
def get_absolute_url(self):
return reverse('article_list')
This is urls.py:
from django.urls import path
from . import views
from .models import Category
urlpatterns = [
path('', views.ArticleViewList.as_view(), name='article_list'),
path('<int:pk>/edit/', views.ArticleUpdateView.as_view(), name='article_edit'),
path('<int:pk>/delete/', views.ArticleDeleteView.as_view(), name='article_delete'),
path('<int:pk>', views.ArticleDetailView.as_view(), name='article_detail'),
path('new/', views.ArticleCreateView.as_view(), name='article_new'),
]
This is class-based view for returning the list of articles:
class ArticleViewList(LoginRequiredMixin, ListView):
model = models.Article
template_name = 'article_list.html'
login_url = 'login'
1)I want that in the adress-bar was displaying the title of the category 2)I want to know is it good decision to use class-based views 3)I would like to know how to do in template loop for displaying articles of the particular category