Reputation: 418
I want to create a simple html login form with django forms, but whenever I run the server the form does not show, only a button.
This is what I have in my forms.py
file
from django import forms
class LogForm(forms.Form):
email = forms.CharField(label='email')
psword = forms.CharField(label='pasword')
Then in views.py
from django.shortcuts import render
from django.http import HttpResponse
from blog.forms import LogForm
def get_info(request):
form = LogForm()
return render(request, 'login.html', {'form': form})
And finally in login.html
<h1>LOGIN PAGE!</h1>
<form method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
And the only thing that shows is this
View of the webpage with only the sumbit button
Thanks for the help
Upvotes: 0
Views: 1078
Reputation: 914
You can use a FormView
forms.py
from django import forms
class LogForm(forms.Form):
email = forms.CharField(label='email')
psword = forms.CharField(label='pasword')
views.py
from myapp.forms import LogForm
from django.views.generic.edit import FormView
class LogView(FormView):
template_name = 'login.html'
form_class = LogForm
success_url = '/ /'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
return super().form_valid(form)
login.html
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
Note if you are writing a custom view you can use this method, read more about this in the documentation
Django also has ready packaged views for authentication
that you can use. You can read more about that here
Upvotes: 1