quick problem with str and int

9 views
Skip to first unread message

MikeKJ

unread,
Apr 29, 2013, 9:40:45 AM4/29/13
to django...@googlegroups.com
So unfortunately due to historical reason a numerical reference is set as a CharField in the model now I want to pass that reference through a url to json serialise something so

<a href="/json/{{ reference }}/>ddddd</a>  as in /json/810044/

so I used /json/{{ reference|add:"0" }}/  to make it an int in the template

but I still get

Exception Type: TypeError at /json/810044/
Exception Value: 'str' object is not callable

the url line is

(r'^json/(?P<this_ref>.*)/$', '/views/json'),

I am not entirely sure where it is complaining about str being uncallable but having cast it to int in the a href in the template  surely it should have resolved?  Any clues please?

Shawn Milochik

unread,
Apr 29, 2013, 9:49:47 AM4/29/13
to django...@googlegroups.com
How about adding a get_absolute_url method to your model? Then you can take care of the logic there, instead of the template.


Worst-case, you can just do the conversion in your view and assign it as a new property to your model. Since it's just a Python class instance, you can do my_obj.temp_url = (whatever) and then your template can access my_obj.temp_url. 

Addy Yeow

unread,
Apr 29, 2013, 9:49:19 AM4/29/13
to django...@googlegroups.com
You would want to match this, (?P<this_ref>.*),  against a string not integer.




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

MikeKJ

unread,
Apr 29, 2013, 10:04:26 AM4/29/13
to django...@googlegroups.com

Good idea so: 

In the model added

    def get_json_reference(self):
        return "/json/%i/" % int(self.reference)

called <a href="{{ v.get_json_reference }}">  which does produce the correct url for all in the for loop but still get

MikeKJ

unread,
Apr 29, 2013, 10:06:46 AM4/29/13
to django...@googlegroups.com
I did try that against just the string and got the same error hence the cast to int attempt, when cast to int I am using
(r'^json/(?P<this_ref>\d+)/$', '/views/json'),


Reply all
Reply to author
Forward
0 new messages