Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

Django: How to render HTML template based on tuple choice

I have created a model, views and templates like so:

MODEL

project_choices = (
    ('Speaker', (
        ('help', 'Freedom'),
    )),
    ('Money', (
        ('invest', 'Investment'),
    )
    ),
    (
        'Children', (
            ('mc', 'Mother & Child'),
        )
    ),
)


class Blog(models.Model):
    title = models.CharField(max_length=250)
    description = CKEditor5Field('Text', null=True)
    limitation = models.CharField(
        null=True, max_length=50, choices=project_choices)

    def __str__(self):
        return self.title

Now the VIEW

def view_portfolio(request):
    blog= Blog.objects.all()
    template = 'blog/blog.html'
    context = {'blog': blog}
    return render(request, template, context)

then the hmtl template

        {% for blog in blog%}
            {% if blog.limitation['**help**'] %}**//I have also tried {% if portfolio.limitation == project_choices['AI']%}**
            <div class="col-lg-4 col-md-6">
              <div class="portfolio-wrap">
                <img src="{{blog.featured}}" class="img-fluid" alt="">
                <div class="blog-info">
                  <h4>{{blog.title}}</h4>
                  <p></p>
                  <div class="portfolio-links">
                    <a href="{{blog.featured}}" data-gall="blogGallery" class="venobox" title="{{blog.title}}"><i class="bx bx-plus"></i></a>
                    <a href="blog-details.html" title="More Details"><i class="bx bx-link"></i></a>
                  </div>
                </div>
              </div>
            </div>
            {% endif %}  
      {%endfor%}

My goal is to show blogs based on ONLY help as the chosen choice please how can I achieve this? I am using django3

question from:https://stackoverflow.com/questions/65651083/django-how-to-render-html-template-based-on-tuple-choice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...