1

I would like to improve my django message with some things : add a breakline in the text and add an url using django reverse url.

This is my message :

messages.error(self.request, _(  f"The link to download the document has expired. Please request it again in our catalogue : {redirect to freepub-home}" 

I would like to separate my message by adding a new line after the . in order to get something like this :

messages.error(self.request, _(  f"The link to download the document has expired. 
                                   Please request it again in our catalogue : {redirect to freepub-home}" 

Then, how I can set django redirection in my message thanks to reverse url to "freepub-home" ?

Thank you by advance !

EDIT :

I overcome to set breakline :

messages.error(self.request, mark_safe(
            "The link to download the document has expired." 
            "<br />"
            "Please request it again in our catalogue : 
            <a href='{% url "freepub-home" %}'> my link </a>")

But I don't find up to now how I can pass django url inside because I have an issue with quote and double quotes.

ChocoBomb
  • 301
  • 4
  • 15

1 Answers1

2

What you're passing to mark_safe is a plain string, it's not interpreted as a Django template, so you cannot use template tags syntax inside it. You have to use the reverse() function to get the url and python string formatting syntax to build the message:

from django.core.urlresolvers import reverse

# ...

    # using triple-quoted string makes life easier
    msg = """
        The link to download the document has expired. 
        <br />
        Please request it again in our catalogue : 
        <a href='{url}'> my link </a>
        """
    url = reverse("freepub-home")
    messages.error(self.request, mark_safe(msg.format(url=url)))
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118