我想知道谁可以检查帖子是否已付款,并相应显示内容content和content_preview的内容。
models.py
class Post_Sell_Multiple(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content_preview = models.TextField(verbose_name="Post Content Preview", max_length=500)
content = EncryptedTextField(verbose_name="Post Content", max_length=10000)
tag = models.CharField(verbose_name="Tags/Meta", max_length=50, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
price_usd = IntegerRangeField(verbose_name="Post Price", min_value=1, max_value=999999, null=False)
post_type = models.CharField(default='Sell Multiple', editable=False, max_length=13)
postattachment = models.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment_sell]
)
postcover = models.ImageField(
verbose_name="Post Cover",
null=True,
blank=True,
upload_to=get_file_path_user_uploads,
validators=[default_image_size, default_image_file_extension],
)
up_vote = models.IntegerField(verbose_name='Post Up-Vote(s)', default=0)
down_vote = models.IntegerField(verbose_name='Post Down-Vote(s)', default=0)
published_date = models.DateField(auto_now_add=True, null=True)
UNPAID_STATUS = 0
PAID_STATUS = 1
POST_PAYMENT_STATUS = (
(UNPAID_STATUS, 'unpaid'),
(PAID_STATUS, 'paid')
)
class Post_Paid_Sell_Multiple(models.Model):
paying_user = models.ForeignKey(User, on_delete=models.CASCADE)
post_sell_multiple = models.ForeignKey(Post_Sell_Multiple, on_delete=models.CASCADE)
STATUS = models.IntegerField(choices=POST_PAYMENT_STATUS, default=0)
paid_date = models.DateField(auto_now_add=True)
def publish(self):
self.paid_date = timezone.now()
self.save()
class Meta:
ordering = ['-paid_date']
views.py
def post_sell_multiple_detail(request, pk):
post = get_object_or_404(Post_Sell_Multiple, pk=pk)
list_comments = Post_Sell_Multiple_Comment.objects.get_queryset().filter(post_id=pk).order_by('-pk')
paginator = Paginator(list_comments, 10)
page = request.GET.get('commentpage')
comments = paginator.get_page(page)
return render(request, 'app/Post_Sell_Multiple/post_sell_multiple_detail.html', {'post': post, 'comments': comments})
但是,如果我手动将帖子设置为在模板上不支付任何更改,而我仍然找不到问题
template.html
<div>
{% if post.STATUS == post.PAID_STATUS %}
<p>{{ post.content|safe|linebreaksbr }}</p>
{% else %}
<p>{{ post.content_preview|safe|linebreaksbr }}</p>
{% endif %}
<div class="clearfix"></div>
</div>
我想我的问题是我不知道如何在模板的if语句中调用状态。当前,如果不存在付费记录,它将始终显示付费状态及其内容,而不是内容预览
非常感谢
答案 0 :(得分:0)
<div>
{% if post.get_STATUS_display|lower == 'paid' %}
<p>{{ post.content|safe|linebreaksbr }}</p>
{% else %}
<p>{{ post.content_preview|safe|linebreaksbr }}</p>
{% endif %}
<div class="clearfix"></div>
</div>
答案 1 :(得分:0)
尝试以下
{% if post.STATUS == 1 %} <<<<<<
或者,您可能会发现方便地定义一些其他变量,例如
UNPAID_STATUS = 0
PAID_STATUS = 1
POST_PAYMENT_STATUS = (
(UNPAID_STATUS, 'unpaid'),
(PAID_STATUS, 'paid)
)
然后做
{% if post.STATUS == post.PAID_SATUS %}
答案 2 :(得分:0)
快速而肮脏的解决方案:
views.py
def post_sell_multiple_detail(request, pk):
post = get_object_or_404(Post_Sell_Multiple, pk=pk)
list_comments = Post_Sell_Multiple_Comment.objects.get_queryset().filter(post_id=pk).order_by('-pk')
status = Post_Paid_Sell_Multiple.objects.filter(user=request.user, post_id=pk, status=1).count()
paginator = Paginator(list_comments, 10)
page = request.GET.get('commentpage')
comments = paginator.get_page(page)
return render(request, 'app/Post_Sell_Multiple/post_sell_multiple_detail.html',
{'post': post,
'comments': comments,
'status': status
})
template.html
{% if status == 1 %}
<p>{{ post.content|safe|linebreaksbr }}</p>
{% else %}
<p>{{ post.content_preview|safe|linebreaksbr }}</p>
{% endif %}
models.py
class Post_Paid_Sell_Once(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post_Sell_Once, on_delete=models.CASCADE)
status = models.IntegerField(choices=POST_PAYMENT_STATUS, default=0)
paid_date = models.DateField(auto_now_add=True)
class Meta:
unique_together = ('user', 'post')
def publish(self):
self.paid_date = timezone.now()
self.save()
class Meta:
ordering = ['-paid_date']