简体   繁体   中英

DjangoCMS - Render database data to html

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

I think you can do it like this!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM