djangodynamic-data

do not show the dynamic data in the tag list


[enter image description here](https://i.sstatic.net/g5sdk.jpg)

in the image Turkish words: Kategori = category , Etiket = Tag

I want to dynamically see the categories and tags I marked from the admin panel from the category and tag template, but they do not appear. I do not understand where I dıd wrong.

topics html codes

html

{% extends 'partials/_base.html' %}
{% load static %}

{% block content %}
    
    <div class="all-title-box">
        <div class="container text-center">
            <h1>Courses</span></h1>
        </div>
    </div>
    
    <div id="overviews" class="section wb">
        <div class="container">
            <div class="row"> 
                <div class="col-lg-9 blog-post-single">
                    
                    <div class="row" style=""> 
                        {% for topic in topics %}
                        <div class="col-lg-6 col-md-6 col-12 mb-4">
                            <div class="course-item">
                                <div class="image-blog">
                                    <img src="{{topic.image.url}}" alt="" class="img-fluid">
                                </div>
                                <div class="course-br">
                                    <div class="course-title">
                                        <h2><a href="{% url 'topic_detail' topic.category.slug topic.id %}" title="">{{topic.name}}</a></h2>
                                    </div>
                                    <div class="course-desc">
                                        <p>{{topic.description | truncatechars:100}}</p>
                                    </div>
                                </div>
                                <div class="course-meta-bot">
                                    <ul>
                                        <li><i class="fa fa-calendar" aria-hidden="true"></i> {{topic.date|date:"d M Y"}}</li>
                                        <li><i class="fa fa-book" aria-hidden="true"></i> {{topic.writer}}</li>
                                    </ul>
                                </div>
                            </div>
                        </div><!-- end col -->
                        {% endfor %}
                    </div><!-- end row -->
                
                    
                </div><!-- end col -->
                <div class="col-lg-3 col-12 right-single">
                    <div class="widget-search">
                        <div class="site-search-area">
                            <form method="get" id="site-searchform" action="#">
                                <div>
                                    <input class="input-text form-control" name="search" placeholder="Anahtarkelime ara..." type="text">
                                    <input id="searchsubmit" value="Search" type="submit">
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="widget-categories">
                        <h3 class="widget-title">Kategori</h3>
                        <ul>
                            {% for category in categorys %}
                                <li><a href="{% url 'topic_by_category' category.slug %}">{{category.name}}</a></li>
                            {% endfor %}
                        </ul>
                    </div>
                    <div class="widget-tags">
                        <h3 class="widget-title">Etiket</h3>
                        <ul class="tags">
                            {% for tag in tags %}
                                <li><a href="{% url 'topic_by_tag' tag.slug %}">{% if tag.slug in request.path %} <b>{{tag.name}}</b>{% else %}{{tag.name}}{% endif %}</a></li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            </div><!-- end row -->
        </div><!-- end container -->
    </div><!-- end section -->

    {% endblock %}


views.py codes

python

from django.shortcuts import render
from . models import Topic,Category,Tag


def topic_list(request):
    topics = Topic.objects.all().order_by('-date')
    categories = Category.objects.all()
    tags = Tag.objects.all()
    
    context = {
        'topics' : topics,
        'categories' : categories,
        'tag' : tags
    }
    return render(request, 'topics.html', context)

def topic_detail(request, topic_slug, topic_id):
    topics = Topic.objects.get(category__slug = topic_slug, id = topic_id)

    context = {
        'topics' : topics
    }
    return render(request, 'topic.html', context)

def category_list(request, category_slug,):
    topics= Topic.objects.all().filter(category__slug = category_slug)
    categories = Category.objects.all()
    tags = Tag.objects.all()

    context = {
        'topics' : topics,
        'categories' : categories,
        'tag' : tags
    }
    return render(request, 'topics.html', context)

def tag_list(request, tag_slug,):
    topics= Topic.objects.all().filter(tag__slug = tag_slug)
    categories = Category.objects.all()
    tags = Category.objects.all()

    context = {
        'topics' : topics,
        'categories' : categories,
        'tag' : tags
    }
    return render(request, 'topics.html', context)

models.py codes

python

from django.db import models

# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=50, null= True)
    slug = models.SlugField(max_length=50, unique= True ,null= True)
    
    def __str__(self):
        return self.name


class Tag(models.Model):
    name = models.CharField(max_length=50, null= True)
    slug = models.SlugField(max_length=50, unique= True ,null= True)
    
    def __str__(self):
        return self.name

class Topic(models.Model):
    name = models.CharField(max_length=200, unique=True, verbose_name="konu adı", help_text="konu başlğı girin")
    category = models.ForeignKey(Category, null=True, on_delete= models.DO_NOTHING)
    tag = models.ManyToManyField(Tag,blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to= "topics/%Y/%m/%d/",default= "default.jpg" )
    date = models.DateField(auto_now= True)
    available = models.BooleanField(default= True)
    
    def __str__(self):
        return self.name

urls.py codes

python

from django.urls import path
from . import views

urlpatterns = [
    path('', views.topic_list, name = "topics"),
    path('<slug:category_slug>/<int:topic_id>', views.topic_detail, name = "topic_detail"),
    path('categories/<slug:category_slug>', views.category_list, name = "topic_by_category"),
    path('tags/<slug:tag_slug>', views.tag_list, name = "topic_by_tag"),

]

admin.py codes

python

from django.contrib import admin
from . models import Topic, Category, Tag
# Register your models here.

@admin.register(Topic)
class TopicAdmin(admin.ModelAdmin):
    list_display = ("name", "available")
    list_filter = ("available",)
    listfield = ("name", "desciription")

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields={'slug': ('name',)} #slug alanını name alanından aldıdık 

@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    prepopulated_fields={'slug': ('name',)} 

How can I fix this problem? I want to see as dynamic tags and categories


Solution

  • I noticed, I did some wrong on the views.py So I exchanged function with as class

    from django.shortcuts import render,get_object_or_404
    from . models import Topic,Category,Tag
    
    def topic_list(request, category_slug = None, tag_slug = None):
        category_page = None
        tag_page = None
        category = Category.objects.all()
        tag = Tag.objects.all()
    
        if category_slug != None:
            category_page = get_object_or_404(Category, slug = category_slug)
            topics = Topic.objects.filter(available = True, category = category_page)
        
        elif tag_slug != None:
            tag_page = get_object_or_404(Tag,slug = tag_slug)
            topics = Topic.objects.filter(available = True, tag = tag_page)
        
        else:
            topics = Topic.objects.all().order_by("-date")
    
        context = {
            'topics' : topics,
            'categories' : category,
            'tags' : tag
            }
        return render(request, 'topics.html', context)
        
    def topic_detail(request, category_slug, topic_id):
        topics = Topic.objects.get(category__slug = category_slug, id = topic_id)
    
        context = {
            'topics' : topics
        }
        return render(request, 'topic.html', context)
    
    def search(request):
        topics = Topic.objects.filter(name__contains = request.GET['search'])
        category = Category.objects.all()
        tag = Tag.objects.all()
    
        context = {
            'topics' : topics,
            'categories' : category,
            'tags' : tag
            }
        return render(request, 'topics.html', context)