Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a link into a Django error message

I want to insert a link to a page inside of my project using the Django error messages something like this

 messages.error(request, 'Please click <a href="{% url 'myproject:settings' %} >here </a>')
like image 977
jfk83 Avatar asked Jan 05 '23 23:01

jfk83


1 Answers

Try using format_html:

from django.utils.html import format_html
from django.core.urlresolvers import reverse


message = format_html('Please click <a href="{}">here</a>', reverse('myproject:settings'))
messages.error(request, message)

Using format_html means that the string will be marked as safe, so the <a> tag should work. Note that I've used reverse rather than {% url %}, because treating the string as Django template language would be trickier.

like image 192
Alasdair Avatar answered Jan 11 '23 21:01

Alasdair