Ensure submitted slugs do not conflict with existing resolvable URLs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Somewhere in your application:

from registration.forms import RegistrationForm
from django.forms import ValidationError
from django.core.urlresolvers import resolve, Resolver404
from urlparse import urlparse

class NewRegistrationForm(RegistrationForm):

    # Ensures than any usernames added will not
    # conflict with existing commands.

 def clean_username(self):
        username = super(NewRegistrationForm, self).clean_username()
        try:    resolve(urlparse('/' + username + '/')[2])
        except Resolver404, e:
            return username

        raise ValidationError(
            (u'This username does not create '
             u'a valid URL.  Please choose '
             u'another'))


# in urls.py:

url(r'^accounts/register/$',
    'registration.views.register',
    { 'form_class': NewRegistrationForm },
    name = 'registration_register')

More like this

  1. Extend simplejson to understand closures, functors, generators and iterators by ElfSternberg 3 years, 12 months ago
  2. Ensure ugettext is available absolutely everywhere by ElfSternberg 3 years, 10 months ago
  3. Generate Google Calendar links from django-event-calendar by ElfSternberg 2 years, 7 months ago
  4. Effective content caching for mass-load site using redirect feature by nnseva 1 year, 10 months ago
  5. slug_and_slash_to_dash - modified slugify for urls by bradmontgomery 2 years, 8 months ago

Comments

(Forgotten your password?)