How to decode a https response url in django

Hello there,

I want to be able to decode a django request url before being match with the paths in Thanks

Why? What is your root objective for doing this?

This is the path for my account activation :path(‘activate/(?P[0-9A-Za-z_-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/’,views.activate, name=‘activate’),

The tokens for account authentication works perfectly in local environment but in production server it doesn’t.
I also notice that query with space in search returns negative result in production but works fine in local environment.

I feel the reason is because of the space in between the token is represented by ‘%20’ that’s the reason for the error

A space in a url is invalid. Spaces are not part of a url, they must be encoded as %20. If you’re generating a token that contains a space, then you’ve got something else wrong that needs to be fixed.
(I also don’t see where a space would match that regex.)

But what could be the cause…
I’ve review my code and nothing seems to be wrong.

Everything works perfectly in the local environment but brings url not match in production server

You’re not showing any code or data here. It’s impossible for us to help you with the issue without any details about what you’re trying to do.

Oh…so sorry.

here is my url path:

 path('activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/',views.activate, name='activate'),

Here is the view for activation of token

def activate(request, uidb64, token):
		User = get_user_model()
			uid = force_str(urlsafe_base64_decode(uidb64))
			user = User.objects.get(pk=uid)
		except(TypeError, ValueError, OverflowError, User.DoesNotExist):
			user = None
		if user is not None and account_activation_token.check_token(user, token):
			user.is_active = True
			if user.is_staff == False:
				merged= {
                             return HttpResponse('Thank you for your email confirmation. Now you can login your  account. Here:')
			return HttpResponse('Activation link is invalid!')

from django.contrib.auth.tokens import PasswordResetTokenGenerator  
import six  
class TokenGenerator(PasswordResetTokenGenerator):  
    def _make_hash_value(self, user, timestamp):  
        return (  
            six.text_type( + six.text_type(timestamp) +  
account_activation_token = TokenGenerator() 

email template for activation

{% autoescape off %}  
Hi {{user}},  

Please click on the link to confirm your registration,  

https://{{ domain }}{% url 'activate' uidb64=uid token=token %}  
{% endautoescape %} 

And you’re saying that you’re getting a space in there?

Can you post a sample of the email being generated?

No space in the url generated

the domain + activate/(%3FPOTc%5B0-9A-Za-z_%5C-%5D+)/(%3FPc1rmz7-c3d43a7bacbca30724ad5475213f6df7%5B0-9A-Za-z%5D%…

But this doesn’t work on the production server

It’s not surprising to me that it doesn’t work on production. What surprises me is that you have any environment where these urls are working, if these are the urls being generated.

You need to find out where in your code you’re generating these non-url-valid characters and fix it. %3F is a question mark, %5B and %5D are [ and ], while %5C is \. You also shouldn’t have the ( and ) around the url components.

Thank you so much…

is it from my or I have to make the changes?

Neither. I’m guessing that the root issue is in how you’re creating your token.


Pls, do you have a resource can I use for a better token generation

I’d start with the official Django source for this. That’s the best resource I can think of.


Thank you so much for helping me…

I will let you know once I solve it

This is literally your path definition (in Looks like when you generate url, the variables parts of the url are not expanded with the values you give for uidb64 and token.

Looking at your code (use of six.text_type, use of path function with regexps in urls), you are using Django 1 with compatibility code for python 2 and 3.

Are you sure you use the same version of django in local environement and your production environment. E.g. : if using Django 2 or above in production environment, your path definition would be wrong (it should be a re_path), and the fact the parameters are not expanded by the

sounds like such confusion to me (even if I didn’t test it and I may also expect it fails in a different way)

What about the issue with the query search?

What issue with the query search?

antoinehumbert raises an excellent point, and one I didn’t catch.

At a minimum, this:

path('activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/',views.activate, name='activate'),

should be:

re_path('activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/',views.activate, name='activate'),

That would explain why the urls aren’t being created correctly.

Try fixing that and we’ll see if there are any other issues outstanding.

Yes, I’m using the same django version on both server. Version 4.1.5