Search code examples
djangodjango-templatesdjango-viewsdjango-urlsdjango-registration

Passing email to path in django


I have code like this:-

path(something/regex_email/, views.func, name="something"),

and if user enters any email in url like something /anyemail/ then the user should be redirected to some page...

But it's not redirecting me to that page and I have seen some previous suggestions regarding to these kinds of questions but those are not working.

views.py

def users(request):
if request.method == 'POST':
    user_email = request.POST.get('').rsplit('/',1)
    print(user_email)
    email = UserApprovedModel.objects.get(email__exact=user_email)
    form = UserRegisterForm(request.POST)
    form.email = email
    if form.is_valid():
        obj = form.save()
        obj.org = request.user.profile.org
        obj.save()
        return redirect('login')
        # username = form.cleaned_data.get('username')
        # messages.success(request, f'Your account has been created! You are now able to log in')
        # return redirect('login')
else:
    form = UserRegisterForm()
user_org = request.user.profile.org
profiles = Profile.objects.filter(org = user_org)
context = {'form': form, 'user_org': user_org.name, 'profiles': profiles}
return render(request, 'users/register.html', context)

Solution

  • So, I got my answer I wanted that in the end of the path if a user give a valid email id which is not in my database, so I want to redirect that user on that page which I want to, so I got that solution with using re_path() function which allows you to give regex path, so this how I used it:

    from django.urls import path, include, re_path
    from users.views import usersAcc
    
    urlpatterns = [
    re_path(r'^users/\w+|[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$/', usersAcc, name='org-users'),
    ]
    
    

    ('something//', views.yourView, name='something')