1👍
✅
There is no need to involve urls in this case (if I understand what you are doing correctly).
Just use something like this instead (assuming Django 1.5 or higher):
from django.contrib import auth
def clean_username(username):
url = urlresolvers.reverse('your_view_name', kwargs=dict(username=username))
match = urlresolvers.resolve(url)
if match.view_name != 'your_view_name':
raise ValidationError(_(
u'This username does not create a valid URL. Please choose another'))
if auth.get_user_model().objects.filter(username__iexact=username):
raise ValidationError(_(u'There is already a user with this username'))
return username
Source:stackexchange.com