Pierre-Sassoulas / django-survey

A django survey app that can export results as CSV or PDF using your native language.
GNU Affero General Public License v3.0
217 stars 145 forks source link

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

Open shrawanx opened 3 years ago

shrawanx commented 3 years ago

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 commented 3 years ago

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 :)

shrawanx commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Hi,

anyone solved this problem?

'ImageSelectWidget' object has no attribute 'template_name'

Thanks!

tiagolagrecap commented 3 years ago

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 commented 3 years ago

@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: https://github.com/Pierre-Sassoulas/django-survey/issues/115#issuecomment-753309993 But still i couldn't get it work.

The problem i faced

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 commented 3 years ago

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

`