How to use characters like . in username slug in Django 2.1?

1.6k Views Asked by At

I am creating a simple web app. The user suppose to signup and can see their profile. The signup form is working fine and the user can signup perfectly.

In the forms.py I have function to raise validation error when the user signup with an existing username. Here is the problem. If there is an username types for example - 'userone' , and another user types 'user.one' it saves the new user with 'user.one'. But when the user wants to go to their profile, the problem arises in the URL. Because I am using username as slug, the dot(.) is not present in the URL which leads to the problem.

I have tried with re_path as mentioned in the Django document, but still getting errors.

forms.py to check unique username

    def clean_username(self):
    username = self.cleaned_data.get('username')
    email = self.cleaned_data.get('email')

    if username and User.objects.filter(username=username).exclude(email=email).count():
        raise forms.ValidationError('This username already exists')
    return username

views.py (signup class)

class SignupView(View):
form_class = SignupForm
template_name = 'webapp/user_signup.html'

def get(self, request):
    form = self.form_class(None)
    return render(request, self.template_name, {'form':form})

def post(self, request):
    form = self.form_class(request.POST)

    if form.is_valid():
        user = form.save(commit=False)
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()


        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('webapp:home')

views.py (for profile class)

class ProfileView(generic.DetailView):
model = User
slug_field = 'username'
template_name = 'webapp/user_profile.html'

urls.py

urlpatterns = [
# user signup link
path('user/signup/', views.SignupView.as_view(), name='signup'),

. . . .. . . .. . . .. 

# user profile view
path('user/<slug:slug>/', views.ProfileView.as_view(), name='user_profile'),

]

HTML

<a href="{% url 'webapp:user_profile' article.user.username %}">{{article.user.first_name}} {{article.user.last_name}}</a>

What should be a perfect approach for this?

1

There are 1 best solutions below

2
On BEST ANSWER

The only problem here is that your URL pattern doesn't accept dots. You could change it to a str, which accepts anything:

path('user/<str:slug>/',

Or use a regex if you want to be a bit more selective about what you accept:

re_path(r'user/(?P<slug>[\w.]+)/$',