I am building a FAQ plugin that will display a list of questions and answers on my Django CMS site. I have the following model:
class Faq(models.Model):
question = models.CharField(
'question',
blank=False,
default="",
help_text=u'Please type in the question',
max_length=256,
)
answer = HTMLField(configuration='CKEDITOR_SETTINGS_BASIC',
null=True,
help_text=u'Please provide an answer. if you paste HTML make sure to cmd+shift+v for plain paste')
def __unicode__(self): # Python 3: def __str__(self):
return self.question
Which I defined in the admin:
class FaqAdmin(admin.ModelAdmin):
model = Faq
extra = 3
admin.site.register(Faq, FaqAdmin)
And I've added a few instances as content.
For now in the plugin defines 10 questions:
class FaqPluginModel(CMSPlugin):
faq1 = models.ForeignKey(Faq, related_name='faq1+')
faq2 = models.ForeignKey(Faq, related_name='faq2+')
faq3 = models.ForeignKey(Faq, related_name='faq3+')
faq4 = models.ForeignKey(Faq, related_name='faq4+')
faq5 = models.ForeignKey(Faq, related_name='faq5+')
faq6 = models.ForeignKey(Faq, related_name='faq6+')
faq7 = models.ForeignKey(Faq, related_name='faq7+')
faq8 = models.ForeignKey(Faq, related_name='faq8+')
faq9 = models.ForeignKey(Faq, related_name='faq9+')
faq10 = models.ForeignKey(Faq, related_name='faq10+')
def __unicode__(self):
return self.faq1.question
However this approach is not scalable. I am looking for a way to fetch all the models from the admin and render them in the template's html. Something along the lines of:
<div class="col-xs-12 col-sm-6">
<div class="box">
--->> {% for every faq model render this: %} <<--
<div class="question">
<div class="question-title">
What are your supported platforms?
</div>
<div class="question-arrow">
</div>
</div>
{% endfor %}
My suggestion is , write a view and create object for the model and render to template html.
For example:
from models import Faq
def faq_view(request):
faqs = Faq.objects.all()
return render_to_response('faq.html', {'faqs': faqs})
Then in faq.html write a for loop and display like below.
<ul>
{% for faq in faqs %}
<li><strong>{{faq.question}}</strong></li>
<li>{{faq.answer}}</li>
{% endfor %}
</ul>
I hope this will help you.