Home > Net >  DjangoCMS - Render database data to html
DjangoCMS - Render database data to html

Time:11-14

I'm testing out the DjangoCMS but I cannot understand how to create custom plugins that enable rendering data out of the database. I'd like to create a plugin that shows all the users for example but I can't understand how to pass the data into the HTML file. I followed the DJangoCMS documentation to create a custom plugin that shows the guest name, I've already tried to add users = User.objects.all() and then pass it to the render function but I receive an argument exception because the render function does not allow so many arguments, so what's the approach in DJangoCMS? models.py

from cms.models.pluginmodel import CMSPlugin
from django.db import models    
class Hello(CMSPlugin):
    guest_name = models.CharField(max_length=50, default='Guest')

cms_plugins.py

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User
from .models import Hello

@plugin_pool.register_plugin
class HelloPlugin(CMSPluginBase):
    model = Hello
    name = _("Hello Plugin")
    render_template = "hello_plugin.html"
    cache = False
    def render(self, context, instance, placeholder):
        context = super().render(context, instance, placeholder)
        return context

CodePudding user response:

I think you can do it like this!

context = super().render(context, instance, placeholder)   
users = User.objects.all()
context['users'] = users
return context
  • Related