homepage login form Django

后端 未结 5 1493
攒了一身酷
攒了一身酷 2020-12-13 00:42

I want to create a homepage with a header that asks to login with username/password and a login button to login. Currently, how I have my page set up is that pressing login

5条回答
  •  别那么骄傲
    2020-12-13 01:19

    Using Django 1.11. I had the same problem just now, here's what worked for me...

    Import the login view class from the built in auth app and pass in your template file via the template_name kwarg.

    In urls.py:

    from django.contrib.auth.views import LoginView
    
    app_name = 'yourapp'
    urlpatterns = [
        url(r'^$', LoginView.as_view(template_name='yourapp/index.html'), name="index"),
    ]
    

    And in your view you can use the form variable to render out your form. In my case I use bootstrap so.

    In index.html:

    {% extends 'base.html' %}
    {% loads bootstrap %}
    {% block content %}
        
    {% csrf_token %} {% bootstrap_form form %} {% bootstrap_button "Login" button_type="submit" button_class="btn-primary" %} {# Assumes you setup the password_reset view in your URLconf #}

    Lost password?

    {% endblock content %}

提交回复
热议问题