Как создать список категорий таблеток для медиа-блога?
Почему невозможно, чтобы две медиа-статьи, созданные на основе модели Django (с именем MediaModule
), имели два разных набора категорий?
У меня есть TextField
, который содержит список связанных категорий, разделенных запятой. Я просто хочу иметь возможность перечислить эти категории в моем шаблоне Django. Почему мои статьи продолжают получать одни и те же категории, несмотря на наличие отдельного списка слов в моей базе данных Django?
models.py
:
from django.db import models
import uuid, random, string
def generate_unique_id(charlimit):
'''This will generate a random set of numbers and letters which will be used to generate a unique URL for each object in the model.
'''
random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=charlimit)) # Generates a random string
return f"{random_string}"
def generate_unique_uuid():
'''This will generate a random set of numbers and letters which are to be derrived from the UUID methodology, which will be used to generate a unique URL for each object in the model.
'''
return str(uuid.uuid4())[:8]
# Create your models here.
class MediaModule(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
article_identifier = models.CharField(max_length=50, default=generate_unique_id(12), unique=True, editable=False)
article_headline = models.CharField(max_length=100)
article_body = models.TextField()
article_synopsis = models.TextField(null=True)
article_journalist = models.CharField(max_length=20)
article_date = models.DateTimeField(auto_now=True)
article_image = models.ImageField(upload_to='assets')
article_image_summary = models.TextField(default="", null=True, blank=True)
article_image_credit = models.TextField(default="", null=True, blank=True)
article_pills = models.TextField(default="News")
def get_words_list(self):
return [word.strip() for word in self.article_pills.split(',')]
def __str__(self):
return self.article_headline
views.py
:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.template import loader
from .models import MediaModule
from blauwestadtech import utils
def article_objects():
'''This will create an instance of the model to obtain all values from the model. It will then return the name of the model instance.s'''
articles = MediaModule.objects.all()
return articles
print(MediaModule.objects.all())
def article_list(request):
'''This denotes the list of available articles and press releases'''
articles = article_objects()
meta_object__title = utils.meta_object__title("Press & Media")
meta_object__narrative = utils.meta_object__narrative("Stay updated with our latest news and announcements on our Press and Media page. Access press releases, media contacts, and resources for journalists and media professionals.")
meta_object__image = utils.meta_object__image()
meta_object__url = utils.meta_object__url(request)
template = loader.get_template('media-home.html')
context = {
'articles' : articles,
'meta_object__title' : meta_object__title,
'meta_object__narrative' : meta_object__narrative,
'meta_object__image' : meta_object__image,
'meta_object__url' : meta_object__url
}
return HttpResponse(template.render(context, request))
def details(request, article_identifier):
articles = article_objects()
article = get_object_or_404(MediaModule,article_identifier=article_identifier)
meta_object__title = utils.meta_object__title(article.article_headline)
meta_object__narrative = utils.meta_object__narrative(article.article_synopsis)
meta_object__image = utils.meta_object__image(article.article_image.url)
meta_object__url = utils.meta_object__url(request)
template = loader.get_template('media-article.html')
all_media_modules = MediaModule.objects.all()
words_lists = [media_module.get_words_list() for media_module in all_media_modules]
context = {
'article' : article,
'articles' : articles,
'meta_object__title' : meta_object__title,
'meta_object__narrative' : meta_object__narrative,
'meta_object__image' : meta_object__image,
'meta_object__url' : meta_object__url,
'fruits' : ['Apple', 'Banana', 'Cherry'],
'words_lists' : words_lists
}
return HttpResponse(template.render(context, request))
media-article.html
:
{% for words_list in words_lists %}
<ul>
{% for word in words_list %}
<li>{{ word }}</li>
{% endfor %}
</ul>
{% endfor %}