0

I am using Django rest framework rest-auth for login and sign up. But I do not want to use the browseable API as my UI. I am building my own form.

so I am using a custom login url like so:

path('rest-auth/customlogin', CustomLoginView.as_view(), name='rest_login'),

where CustomLoginView is defined as :

class CustomLoginView(LoginView):

    def get(self, request):
        return render(request, "api/test_template.html")

now in test_template.html, I would like to have 2 input fields: email and password. the same fields that are shown in the browserable API if I did not overwrite with the template.

I am not sure how to get the request data into the template. (if email and password fields reside in the request data, is this correct?)

test_template.html

{% block content %}
  <div class="row">
    <h3>Login</h3><hr/>
  {# I am not sure how to use input fields here where the data inputted goes in the email and password rest-auth fields#}
  </div>
{% endblock %}
Community
  • 1
  • 1
bcsta
  • 1,963
  • 3
  • 22
  • 61

1 Answers1

0

You need a form defined

class LoginForm (forms.Form)
    username = forms.CharField(widget = forms.TextInput())
    password = forms.CharField(widget = forms.PasswordxInput())

add it to your view

class CustomLoginView(LoginView):
   form_class = LoginForm
    def get(self, request):
        return render(request, "api/test_template.html")

Then in your template

<form method="post" action="">
    {{ form.username}}
    {{form.password }}
</form>
HenryM
  • 5,557
  • 7
  • 49
  • 105
  • what library do I import for this form? `allauth.account.forms` or `django.contib.auth.forms` ? or is it a custom form? in that case how can I implement it? – bcsta Sep 15 '19 at 08:03
  • since your just learning, put it above your view. You will need `from django import forms` at the top of the file – HenryM Sep 15 '19 at 08:09
  • the form is not seen in the template. why is that? also, I am just learning, yes, but eventually this is going to be a live site – bcsta Sep 15 '19 at 08:14
  • try `form=LoginForm` instead of `form_class` – HenryM Sep 15 '19 at 08:29