1

I asked this question last week and haven't got an answer yet. Now I realize that it doesn't point to the real problem. So, I want to write a notification for a user and then render it to the template. My idea is to write all the html in the views.py file and then in the template just use the {html|safe} tag. I want the notifications to be like this:

user voted up your post on topic.

So, I tried to write the html as a string and use the %s to replace the usernames and the names of the topics, like this:

string_to_render = "<a href = '{% url ... %}'> %s </a> voted up your <a href = '{% url ... %}'> post </a> on the <a href = '{% url ... %}'> %s </a>" %(user, topic)

The %s is there to replace the user and the topic names. But python also understands django's "% url" as an unsigned decimal replacement and I get the error:

%u format: a number is required, not unicode

So, if there isn't an easy solution to this problem. I am open to suggestions about how to render notifications. Maybe I shouldn't write the whole notification before rendering it and I should try writing them on html and use many content variables? That would also require many {% if-else %} template tags cause vote-up is only one of the notifications I would like to give to the user. That is just an idea, but surely there is a better way to do it.

Thanks in advance for your help.

Community
  • 1
  • 1
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
  • Why do you want to write the HTML in the views file? We have templates for a reason, and that reason is to avoid exactly the problems you're having. – Daniel Roseman May 26 '14 at 20:42

1 Answers1

1

You need to escape the % near the url template tag:

string_to_render = "<a href = '{%% url ... %%}'> %s </a> voted up your <a href = '{%% url ... %%}'> post </a> on the <a href = '{%% url ... %%}'> %s </a>" % (user, topic)

Though, as @Daniel Roseman noted, it is better to create a template, include it into the main html template and pass user and topic inside the context while rendering.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195