Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snippet_fragment overwrite 'object' value #38

Closed
llherbier opened this issue Sep 12, 2017 · 4 comments
Closed

snippet_fragment overwrite 'object' value #38

llherbier opened this issue Sep 12, 2017 · 4 comments

Comments

@llherbier
Copy link

llherbier commented Sep 12, 2017

description

snippet_fragment template tag overwrites 'object' value in templates and then breaks other django application templates.

steps to reproduce

  1. add an application to django cms using an apphook
  2. add {% snippet_fragment 'myfragment' %} in base.html
  3. render a view of your application that is calling 'object' in the template
  4. in the application template 'object' is now a Snippet instance and not the expected class

below a trivial fix, maybe not the best one.

--- snippet_tags.py.old	2017-09-12 22:21:00.493503349 +0200
+++ snippet_tags.py	2017-09-12 22:46:59.736466428 +0200
@@ -60,6 +60,9 @@
         :rtype: string
         :return: the HTML for the snippet
         """
+        import copy
+        new_context = copy.copy(context)
+
         # Default assume this is directly an instance
         snippet_instance = self.snippet_id_varname.resolve(context)
         # Assume this is slug
@@ -70,7 +73,7 @@
             elif isinstance(snippet_instance, int):
                 snippet_instance = Snippet.objects.get(pk=snippet_instance)
 
-            return mark_safe(self.get_content_render(context,
+            return mark_safe(self.get_content_render(new_context,
                                                      snippet_instance))
 
         # Rely on the fact that manager something went wrong
@fp4code
Copy link
Contributor

fp4code commented Sep 24, 2017

Please can you give a full (preferably simplified) code source for your steps 1 and 3?

Is in your case context argument is a Context or a dict?

@llherbier
Copy link
Author

for step 1, this is achieved using Django-CMS admin interface: create a new page and add an application in the advanced settings.
for step 3, simply load the corresponding page with you browser :)

type(context).name returns 'dict'

@fp4code
Copy link
Contributor

fp4code commented Sep 24, 2017

Thanks, I think the problem is because a dict was used somewhere instead of a Django Context which is a stack of dicts. Please can you give an apphook code triggering your problem?

  • Step 0
virtualenv -p /usr/bin/python3.5 env
source env/bin/activate
pip install djangocms-installer==0.9.7
djangocms mysite
cd mysite
./manage.py runserver

Of course, no application in the menu!

@sveetch
Copy link
Contributor

sveetch commented Jul 10, 2019

I'm confronting this bug exactly as mentionned in this issue details from "steps to reproduce". It does happen on detail views from application "django-blog-zinnia".

Very strange since i'm using zinnia and snippet since a long time but never faced this bug. But recently i upgraded my stack to the last Django and Django-cms and now it happen.

I digged into Zinnia code and i didn't see any usage about Context but still simple dict usage to pass context data. So i'm wondering if it could be an issue in Zinnia or some forgotten case in snippet with context ?

Environment:

  • Python 3.5.2
  • Django 2.1.8
  • DjangoCMS 3.6.0
  • djangocms-snippet 2.2.0
  • django-blog-zinnia 0.20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants