43147

Put HTML into ValidationError in Django

Question:

I want to put an anchor tag into this ValidationError:

Customer.objects.get(email=value) if self.register: # this address is already registered raise forms.ValidationError( _('An account already exists for this email address') )

The anchor tag would be <a href="some/url/to/login">Log in instead</a>

So, the error message when a user inputs an email that already exists would be something like:

'An account already exists for this email address. <a><strong>Log in</strong> instead</a>?'

Thanks in advance!

Answer1:

This worked:

Customer.objects.get(email=value) if self.register: # this address is already registered raise forms.ValidationError( (_(mark_safe('An account already exists for this email address. <a href="#" class="email_error">Log in instead?</a>'))) )

...but also needed this at the top of the Python file:

from django.utils.safestring import mark_safe

Recommend

  • SignalR dependency injection via Spring.Net
  • Phalcon\\Mvc\\Model::validation() and non-model validators
  • Sum and Average of a series of numbers inputed to a text field
  • Spring MVC redirect with custom http headers
  • How do I configure Maven Cargo to use an embedded Tomcat server?
  • Read a file in “chunks” using PHP
  • Generating anchors with PyYAML.dump()?
  • How to synchronize two scrollview in Android?
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How to autopopulate a field in SugarCRM form
  • Element.tagName for python not working
  • Simulate click Geckofx vb,net
  • how to upload multiple files in c# windows application
  • Odoo 10 - Overriding unlink method
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How to handle images sent by a mobile device?
  • Groovy: Unexpected token “:”
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • How to create a file in java without a extension
  • Django rest serializer Breaks when data exists
  • Recording logins for password protected directories
  • Get one-time binding to work for ng-if
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Is there any way to access browser form field suggestions from JavaScript?
  • HTML download movie download link
  • Volley JsonObjectRequest send headers in GET Request
  • MySQL WHERE-condition in procedure ignored
  • Importing jscolor library in angular 2
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • Getting error when using KSoap library to consume .NET web services
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal