How To Search A String With The Url Patterns In Django?
Solution 1:
You can simply try to resolve the address to a view:
from django.core.urlresolvers import resolve
from myapp.views import user_profile_view
try:
my_view = resolve("/%s/" % user_name)
if my_view == user_profile_view:
# We match the user_profile_view, so that's OK.else:
# oops, we have another view that is mapped on that URL# you already have something mapped on this addressexcept:
# app doesn't have such path
EDIT:
you can also make the check in a different way:
defuser_profile_view(request, user_name):
# some code here
user_profile_view.name = "User Profile View"
and then the check above could be:
ifgetattr(my_view, "name", None) == "User Profile View":
...
Solution 2:
you can add custom form field validation. Look at this post. Django and Custom Form validation
raise forms.ValidationError(u'please choose another username')
check and raise errors.
Or you can try setting the following as url for your users,
example.com/user/<username>/
Solution 3:
I don't think you can check this with django.core.urlresolvers.resolve
. Note that it just checks for the pattern for fixed url and not variable part of your url. In your case, you will most likely have 'username' as variable parameter that is passed to view. Pattern for this will match for non-existing username also. So the checking patterns is not good solution.
Better method will be separating out static or other pages in different namespace. e.g. http://example.com/static/about
Or you can have predefined keywords/reserved words for your site e.g. [ "about", ... ] and check it against username while creating user.
Solution 4:
Put your username view at the end of urls.py, so that other url rules will be checked first.
Then the easiest way is to have a list of invalid user names which should be used in user registration validation.
defclean_username(self):
INVALID_USERNAMES = ('about', 'admin', 'register', '...')
username = self.cleaned_data['username']
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
passelse:
raise forms.ValidationError(u'%s already exists' % username )
if username in INVALID_USERNAMES:
raise forms.ValidationError(u'%s is not a valid username' % username )
return username
Solution 5:
firstly, a url rule for usernames:
url(r'^(?P<username>[-\w]+)/$', 'membership.views.profile', name='profile'),
making sure that a username doesn't conflict with an existing url rule is a little harder.
the way I usually handle this is by adding uniqueness to the url:
url(r'^user/(?P<username>[-\w]+)/$', 'membership.views.profile', name='profile'),
if you absolutely must have the url for profiles start with the username then you can try to rake the urls using a method like this one: https://stackoverflow.com/a/2094270/884453 and then make sure that username is both unique against other usernames and against routes
EDIT
as i was writing this someone posted a cool idea for a validator that makes a bunch more sense.
using from django.core.urlresolvers import resolve
to check it for uniqueness is a great solution
from django.core.exceptions import ValidationError
from django.core.urlresolvers import resolve
defvalidate_unique_resolve(value):
urlroute = Truetry:
urlroute = resolve(path, urlconf)
except urlresolvers.Resolver404:
urlroute = Falseif urlroute != False :
raise ValidationError(u'%s is a reserved url pattern' % value)
Post a Comment for "How To Search A String With The Url Patterns In Django?"