Django-CMS插件内联创建后编辑模式不显示问题求助
Let's troubleshoot this step by step — the issue where your "quick links" widget shows up in published mode but is empty in edit mode is almost always related to how your plugin handles related objects or template rendering in the edit context. Here's what to check and fix:
1. Complete and Fix the copy_relations Method
Your provided LienFooterPlugin model has an incomplete copy_relations method, which is critical for Django CMS to properly copy related objects (your quick links) when the plugin is saved or copied. This is likely the root cause of empty content in edit mode.
Corrected Model Code:
from django.db import models from cms.models.pluginmodel import CMSPlugin class LienFooter(models.Model): # Make sure your related model has a ForeignKey pointing to the plugin plugin = models.ForeignKey( LienFooterPlugin, on_delete=models.CASCADE, related_name='lien_footer' # Must match the name you use in templates ) title = models.CharField(max_length=255) # Example field for link text url = models.URLField() # Example field for link URL class LienFooterPlugin(CMSPlugin): langue = models.CharField(max_length=128) def copy_relations(self, oldinstance): # Clear existing relations first to avoid duplicates self.lien_footer.clear() # Copy each related link from the old instance to the new one for lien in oldinstance.lien_footer.all(): lien.pk = None # Reset primary key to create a new copy lien.plugin = self # Associate the copied link with the current plugin instance lien.save() self.lien_footer.add(lien)
2. Verify Your Plugin's Render Template
Ensure your template properly iterates over the related links, even in edit mode. Django CMS uses the same template for both edit and published modes unless you specify a separate preview template.
Example Template (templates/plugins/lien_footer.html):
<div class="quick-links-widget"> <h3>Quick Links</h3> <ul> {% for link in instance.lien_footer.all %} <li><a href="{{ link.url }}">{{ link.title }}</a></li> {% empty %} <li class="text-muted">No links added yet.</li> {% endfor %} </ul> </div>
3. Configure Your Plugin Class Correctly
Make sure your plugin registration class uses the right template and disables caching (to avoid stale data in edit mode):
from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool from .models import LienFooterPlugin @plugin_pool.register_plugin class LienFooterPluginPublisher(CMSPluginBase): model = LienFooterPlugin name = "Quick Links Footer" render_template = "plugins/lien_footer.html" cache = False # Disable cache to ensure edit mode shows fresh data # Optional: Add a custom preview template if needed # preview_template = "plugins/lien_footer_preview.html"
4. Clear Cache and Test
After making these changes:
- Clear Django's cache (
python manage.py clearcacheif using Django's built-in cache) - Refresh the Django CMS edit page, re-add the plugin, and save your changes
Why This Works:
- The
copy_relationsmethod ensures that when you save the plugin in edit mode, all related quick links are properly associated with the plugin instance, so the template can fetch them. - Disabling cache prevents Django CMS from serving stale, empty content in edit mode.
- The template explicitly handles empty states, so you'll see a message instead of a completely empty widget if no links are added.
内容的提问来源于stack exchange,提问作者Francis Campbell

