我是django的新手,我尝试了一些建议,但似乎无法让它发挥作用。我正在尝试创建一个带有类别部分的博客文章模型,并链接到我的模板中的类别。我的代码如下。
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from taggit.managers import TaggableManager
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
class MoneyManager(models.Manager):
def get_queryset(self):
return super(MoneyManager, self).get_queryset().filter(category='money')
class LoveManager(models.Manager):
def get_queryset(self):
return super(LoveManager, self).get_queryset().filter(category='love')
class WeightlossManager(models.Manager):
def get_queryset(self):
return super(WeightlossManager, self).get_queryset().filter(category='weightloss')
def get_image_filename(instance, filename):
title = instance.post.title
slug = slugify(title)
return "post_images/%s-%s" % (slug, filename)
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
CATEGORY_CHOICES = (
('money', 'Money'),
('love', 'Love'),
('weightloss', 'Weightloss')
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_posts')
image = models.ImageField(upload_to='get_image_filename', verbose_name='Image', blank=True)
body = models.TextField()
image2 = models.ImageField(upload_to='get_image_filename', verbose_name='Image', blank=True)
body2 = models.TextField(blank=True)
image3 = models.ImageField(upload_to='get_image_filename', verbose_name='Image', blank=True)
body3 = models.TextField(blank=True)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager()
published = PublishedManager()
category = models.CharField(max_length=10, choices=CATEGORY_CHOICES)
money = MoneyManager()
love = LoveManager()
weightloss = WeightlossManager()
tags = TaggableManager()
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return 'Comment by {} on {}'.format(self.name, self.post)
4e2
aaefb22cf2adc42680d
' -H '
Content - Type: multipart / form - data
' -POST http://127.0.0.1:8000/api/scan_binary/ -F binary_file=@/Users/a.b.gopalakrishna/Desktop/tele.apk -F uuid=a996a142e1ef4a6e9e29dd7f6baf120bfsfds
我希望在我的模板中有一个名为category的部分,它将显示3个类别(金钱,爱情和减肥)。
如何进行操作以及如何对模板进行编码以显示此信息?谢谢!
答案 0 :(得分:0)
您可以使用_meta.get_field.("category").choices
或get_category_display
进行此操作,也可以直接转换CATEGORY_CHOICES
上的字段选择dict
,如下所示:
def convert_dict_choice(self):
category_dict = dict(self.CATEGORY_CHOICES)
return category_dict
因此,您可以在view
上调用response
作为模板的上下文传递,如下所示:
def select_options(request):
category_select = Post().convert_dict_choice()
return render(
request, "template/name_template.html",
{"data_select":category_select}
)
然后,你可以这样做的模板:
{% extends "base.html" %}
{% block content %}
<div class="category">
<h1>Seccion category</h1>
<select name="services" id="services">
{% for category_selectvice in data_select %}
<option>{{category_selectvice}}</option>
{% endfor %}
</select>
</div>
{% endblock content %}
如果您希望更多参考,请访问django Doc django的文档。