yilmazhuseyin
yilmazhuseyin

Reputation: 6612

convert int to str in django template tags

I have to use an int value as str in a template. Here is the code

<a href="{% url staticPageEdit id=item.id%}">{{ item.id}}</a>

But when I run render the template I got TemplateSyntaxError Description of the error is:Caught NoReverseMatch while rendering: Reverse for 'staticPageEdit' with arguments '()' and keyword arguments '{'id': 1L}' not found. My problem here is that item.id is a number value. but my staticPageEdit page is expecting a string. here is de urls.py lines for this url

url(r'^staticPage/Edit/$',views.staticPageEdit,{'id':'-1'},name='staticPageEdit'),
url(r'^staticPage/Edit/(?P<id>/d+)/$',views.staticPageEdit,name='staticPageEdit'),

I want to some how send a string value instead of a number. is that possible?

Upvotes: 0

Views: 6180

Answers (2)

Pablo
Pablo

Reputation: 21

Use stringformat filter. The code would be something like this

<a href="{% url staticPageEdit id=item.id|stringformat:"i"%}">{{ item.id}}</a>

Upvotes: 2

mechanical_meat
mechanical_meat

Reputation: 169304

I think that this regex:

r'^staticPage/Edit/(?P<id>/d+)/$' 

should be

r'^staticPage/Edit/(?P<id>\d+)/$'

Upvotes: 0

Related Questions