Reputation:
I have a register form , in which when a already registered email gets entered , it shows a error message with a login link, when clicked on login link it redirects to login page, here I want to prefill the the email field with that user email. I am using django.
Does anyone know how to do it? I would appreciate some help. Thank you.
my login url in urls.py:
path('login/', views.login, name="login")
my login link in error message:
<a href="login/">login</a>
Please let me what to do add to the link in url to prefill it with the user email ID.
Upvotes: 1
Views: 103
Reputation: 35042
You just have to retrieve the data provided in the URL using request.GET
and use it to prefill your form's initial
in your login view. That would roughly look like this:
def login(request, *args, **kwargs):
initial = {}
if 'email' in request.GET:
initial['email'] = request.GET['email']
form = LoginForm(request.POST or None, initial=initial)
...
Of course you'll have to add the email to the login URL:
<a href="login/[email protected]">login</a>
Upvotes: 1