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

'ImageSelectWidget' object has no attribute 'template_name' #115

Open
shrawanx opened this issue Jan 1, 2021 · 7 comments
Open

'ImageSelectWidget' object has no attribute 'template_name' #115

shrawanx opened this issue Jan 1, 2021 · 7 comments
Labels
bug good-first-issue This issue is doable without a deep understanding of the project hacktoberfest

Comments

@shrawanx
Copy link

shrawanx commented Jan 1, 2021

When trying to render i.e: survey detail page when the survey consist of a Question with type select image then i am getting this error 'ImageSelectWidget' object has no attribute 'template_name'

Am i missing something or is this a bug ?

I am currently using
Django==3.1.4
django-survey-and-report==1.3.31

@Pierre-Sassoulas
Copy link
Owner

It looks like a bug, apparently the ImageSelectWidget is not properly tested. After a quick check it seem template_name is local instead of being a class attribute. You could try moving this line : https://github.com/Pierre-Sassoulas/django-survey/blob/master/survey/widgets.py#L16 to an attribute of the class. I won't have time to fix this myself but I'll review and merge a pull request with a Question with type select image added in the existing fixtures :)

@Pierre-Sassoulas Pierre-Sassoulas added bug good-first-issue This issue is doable without a deep understanding of the project labels Jan 1, 2021
@shrawanx
Copy link
Author

shrawanx commented Jan 4, 2021

I had a quick patch with the solution you provided, but i am getting another error as js/survey.js not found.
This is from https://github.com/Pierre-Sassoulas/django-survey/blob/master/survey/widgets.py#L12

@Pierre-Sassoulas
Copy link
Owner

Sorry @shrawanx I did not see your message. You can either remove the js/survey.js or create an empty one to be able to add our custom js there, both solution work. Thank you for taking time to troubleshoot this.

@tiagolagrecap
Copy link

Hi,

anyone solved this problem?

'ImageSelectWidget' object has no attribute 'template_name'

Thanks!

@tiagolagrecap
Copy link

Hi @shrawanx, i'm trying to fix this problem but it doesn't work, how did you fix it? can you help me?

'ImageSelectWidget' object has no attribute 'template_name'

Thanks.

@shrawanx
Copy link
Author

shrawanx commented Feb 18, 2021

@tiagolagrecap
I am sorry i was quite busy with my work.
I solved the 'ImageSelectWidget' object has no attribute 'template_name' with the help provided from @Pierre-Sassoulas
See: #115 (comment)
But still i couldn't get it work.

The problem i faced

  • Couldn't render the ImageSelectWidget properly.

I will try to see it and work on it when i get some free time until then, please post your solution here if you find solution.

Thanks

@anthonyrandell
Copy link

I could get round the error following Pierre's advice. To get past the render error you need to refer to the class attribute not to the local variable.

https://github.com/Pierre-Sassoulas/django-survey/blob/master/survey/widgets.py#L21

This renders a blank question however not sure how to configure this so doing some more digging.

from django import forms
from django.template.loader import render_to_string

class ImageSelectWidget(forms.widgets.Widget):
    template_name = "survey/forms/image_select.html" # Class attribute not local
    class Media:
        js = (
            "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js",
            "http://maps.googleapis.com/maps/api/js?sensor=false",
            "js/survey.js",
        )

    def render(self, name, value, *args, **kwargs):
        choices = []
        for index, choice in enumerate(self.choices):
            if choice[0] != "":
                value, img_src = choice[0].split(":", 1)
                choices.append({"img_src": img_src, "value": value, "full_value": choice[0], "index": index})
        context = {"name": name, "choices": choices}
        html = render_to_string(self.template_name, context) # Refer to class attribute not local
        return html

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good-first-issue This issue is doable without a deep understanding of the project hacktoberfest
Projects
None yet
Development

No branches or pull requests

4 participants