# myproject/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from accounts import views as accounts_views
from boards import views urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^signup/$', accounts_views.signup, name='signup'),
url(r'^login/$', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(), name='logout'),
url(r'^boards/(?P<pk>\d+)/$', views.board_topics, name='board_topics'),
url(r'^boards/(?P<pk>\d+)/new/$', views.new_topic, name='new_topic'),
url(r'^admin/', admin.site.urls),
]
<!--templates/base.html-->
{% load static %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}Django Boards{% endblock %}</title>
<link href="https://fonts.googleapis.com/css?family=Peralta" rel="stylesheet">
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/app.css' %}">
{% block stylesheet %}{% endblock %} <!-- 这? -->
</head>
<body>
{% block body %} <!-- 这? -->
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container">
   <a class="navbar-brand" href="{% url 'home' %}">Django Boards</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainMenu" aria-controls="mainMenu" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
<div class="collapse navbar-collapse" id="mainMenu">
   {% if user.is_authenticated %}
    <ul class="navbar-nav ml-auto">
    <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      {{ user.username }}
    </a>
    <div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
     <a class="dropdown-item" href="#">My account</a>
      <a class="dropdown-item" href="#">Change password</a>
     <div class="dropdown-divider"></div>
    <a class="dropdown-item" href="{% url 'logout'%}">Log out</a>
    </div>
     </li>
     </ul>
   {% else %}
    <form class="form-inline ml-auto">
    <a href="#" class="btn btn-outline-secondary">Log in</a>
     <a href="{% url 'signup' %}" class="btn btn-primaryml-2">Sign up</a>
     <a href="{% url 'login' %}" class="btn btn-outline-secondary">Log in</a>
     </form>
  {% endif %}
</div>
</div>
</nav>
  <div class="container">
    <ol class="breadcrumb my-4">
     {% block breadcrumb %}
     {% endblock %}
     </ol>
   {% block content %}
   {% endblock %}
   </div>
  {% endblock body %} <!-- 这? -->
  <script src="{% static 'js/jquery-3.2.1.min.js' %}"></script>
  <script src="{% static 'js/popper.min.js' %}"></script>
  <script src="{% static 'js/bootstrap.min.js' %}"></script>
</body>
</html> <!--templates/login.html-->
{% extends 'base.html' %}
{% load static %}
{% block stylesheet %}
<link rel="stylesheet" href="{% static 'css/accounts.css' %}">
{% endblock %} {% block body %}
<div class="container">
<h1 class="text-center logo my-4">
<a href="{% url 'home' %}">Django Boards</a>
</h1>
<div class="row justify-content-center">
<div class="col-lg-4 col-md-6 col-sm-8">
<div class="card">
<div class="card-body">
<h3 class="card-title">Log in</h3>
<form method="post" novalidate>
{% csrf_token %}
{% include 'includes/form.html' %}
<button type="submit" class="btn btn-primary btn-block">Log in</button>
</form>
</div>
<div class="card-footer text-muted text-center">New to Django Boards?
               <a href="{% url 'signup' %}">Sign up</a>
</div>
</div>
   <div class="text-center py-2">
  <small>
   <a href="#" class="text-muted">Forgot your password?</a>
  </small>
   </div>
  </div>
</div>
</div>
{% endblock %}
<!--templates/base_accounts.html-->
{% extends 'base.html' %}
{% load static %}
{% block stylesheet %}
<link rel="stylesheet" href="{% static 'css/accounts.css' %}">
{% endblock %} {% block body %}
<div class="container">
<h1 class="text-center logo my-4">
<a href="{% url 'home' %}">Django Boards</a>
</h1>
{% block content %}
{% endblock %}
</div>
{% endblock %} <!--templates/login.html-->
{% extends 'base_accounts.html' %}
{% block title %}Log in to Django Boards{% endblock %}
{% block content %}
<div class="row justify-content-center">
<div class="col-lg-4 col-md-6 col-sm-8">
<div class="card">
<div class="card-body">
<h3 class="card-title">Log in</h3>
<form method="post" novalidate>
{% csrf_token %}
{% include 'includes/form.html' %}
<button type="submit" class="btn btn-primary btnblock">Log in</button>
</form>
</div>
<div class="card-footer text-muted text-center">New to Django Boards? <a href="{% url 'signup' %}">Sign up</a></div>
</div>
<div class="text-center py-2">
 <small>
 <a href="#" class="text-muted">Forgot your password?</a>
 </small>
</div>
</div>
</div>
{% endblock %} <!--templates/signup.html-->
{% extends 'base_accounts.html' %}
{% block title %}Sign up to Django Boards{% endblock %} {% block content %}
<div class="row justify-content-center">
<div class="col-lg-8 col-md-10 col-sm-12">
<div class="card">
<div class="card-body">
<h3 class="card-title">Sign up</h3>
<form method="post" novalidate>
{% csrf_token %}
{% include 'includes/form.html' %}
<button type="submit" class="btn btn-primary btnblock">Create an account</button>
</form>
</div>
<div class="card-footer text-muted text-center">Already have an account? <a href="{% url 'login' %}">Log in</a></div>
</div>
</div>
</div>
{% endblock %}
<!--templates/includes/form.html-->
{% load widget_tweaks %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
<p{% if forloop.last %} class="mb-0"{% endif %}>{{ error }}</p>
{% endfor %}
</div>
{% endif %} {% for field in form %}
<div class="form-group">
{{ field.label_tag }}   {% if form.is_bound %}
   {% if field.errors %}
   {% render_field field class="form-control is-invalid" %}
   {% for error in field.errors %}
   <div class="invalid-feedback">
    {{ error }}
   </div>
  {% endfor %}
  {% else %}
   {% render_field field class="form-control is-valid" % }
   {% endif %}
   {% else %}
   {% render_field field class="form-control" %}
   {% endif %} {% if field.help_text %}
  <small class="form-text text-muted">
   {{ field.help_text|safe }} <!-- 新的代码 -->
   {{ field.help_text }}
  </small>
{% endif %}
</div>
{% endfor %}
#在boards应用中,创建一个名为templatetags的新文件夹。然后在该文件夹内创建两个名为 init.py 和 form_tags.py的空文件。
# boards/templatetags/form_tags.py from django import template
register = template.Library() @register.filter
def field_type(bound_field):
return bound_field.field.widget.__class__.__name__ @register.filter
def input_class(bound_field):
css_class = ''
if bound_field.form.is_bound:
if bound_field.errors:
css_class = 'is-invalid'
elif field_type(bound_field) != 'PasswordInput':
css_class = 'is-valid'
return 'form-control {}'.format(css_class) <!--templates/includes/form.html--> {% load form_tags widget_tweaks %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
   <p{% if forloop.last %} class="mb-0"{% endif %}>{{ error }}</p>
{% endfor %}
</div>
{% endif %} {% for field in form %}
<div class="form-group">
{{ field.label_tag }}
{% render_field field class=field|input_class %}
{% for error in field.errors %}
  <div class="invalid-feedback">
   {{ error }}
   </div>
{% endfor %}
{% if field.help_text %}
  <small class="form-text text-muted">
   {{ field.help_text|safe }}
   </small>
{% endif %}
</div>
{% endfor %}
myproject/urls.py
url(r'^reset/$',
auth_views.PasswordResetView.as_view(
template_name='password_reset.html',
email_template_name='password_reset_email.html',
subject_template_name='password_reset_subject.txt'
),
name='password_reset'),
url(r'^reset/done/$',
auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html'),
name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html'),
name='password_reset_confirm'),
url(r'^reset/complete/$',
auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html'),
name='password_reset_complete'), <!--templates/password_reset.html-->
{% extends 'base_accounts.html' %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<div class="row justify-content-center">
<div class="col-lg-4 col-md-6 col-sm-8">
<div class="card">
<div class="card-body">
   <h3 class="card-title">Reset your password</h3>
   <p>Enter your email address and we will send you a link to reset your password.</p>
<form method="post" novalidate>
{% csrf_token %}
{% include 'includes/form.html' %}
<button type="submit" class="btn btn-primary btnblock">Send password reset email</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
#templates/password_reset_subject.txt
[Django Boards] Please reset your password <!--templates/password_reset_email.html-->
Hi there,
  Someone asked for a password reset for the email address {{ email }}.
Follow the link below:
  {{ protocol }}://{{ domain }}{% url 'password_reset_confirm'uidb64=uid token=token %}
  In case you forgot your Django Boards username: {{ user.username }}
  If clicking the link above doesn't work, please copy and paste the URLin a new browser window instead.
  If you've received this mail in error, it's likely that another user entered
  your email address by mistake while trying to reset a password.
  If you didn'tinitiate the request, you don't need to take any further action and can safely disregard this email.
                                            Thanks,
                                        The Django Boards Team
<!--templates/password_reset_done.html-->
{% extends 'base_accounts.html' %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<div class="row justify-content-center">
<div class="col-lg-4 col-md-6 col-sm-8">
<div class="card">
<div class="card-body">
<h3 class="card-title">Reset your password</h3>
<p>Check your email for a link to reset your password. If it doesn't appear within a few minutes, check your spam folder.</p>
<a href="{% url 'login' %}" class="btn btn-secondary btn-block">Return to log in</a>
</div>
</div>
</div>
</div>
{% endblock %}
<!--templates/password_reset_confirm.html-->
{% extends 'base_accounts.html' %}
{% block title %}
{% if validlink %}
  Change password for {{ form.user.username }}
{% else %}
  Reset your password
{% endif %}
{% endblock %} {% block content %}
<div class="row justify-content-center">
<div class="col-lg-6 col-md-8 col-sm-10">
<div class="card">
<div class="card-body">
  {% if validlink %}
     <h3 class="card-title">Change password for @{{ form.user.username }}</h3>
    <form method="post" novalidate>
     {% csrf_token %}
     {% include 'includes/form.html' %}
     <button type="submit" class="btn btn-success btn-block">Change password</button>
     </form>
   {% else %}
     <h3 class="card-title">Reset your password</h3>
      <div class="alert alert-danger" role="alert">
     It looks like you clicked on an invalid password reset link. Please try again.
     </div>
   <a href="{% url 'password_reset' %}" class="btn btn-secondary btn-block">Request a new password reset link</a>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}
<!--templates/password_reset_complete.html-->
{% extends 'base_accounts.html' %}
{% block title %}Password changed!{% endblock %} {% block content %}
<div class="row justify-content-center">
<div class="col-lg-6 col-md-8 col-sm-10">
<div class="card">
<div class="card-body">
<h3 class="card-title">Password changed!</h3>
<div class="alert alert-success" role="alert">
You have successfully changed your password! You may now proceed to log in.
</div>
<a href="{% url 'login' %}" class="btn btn-secondary btn-block">Return to log in</a>
</div>
</div>
</div>
</div>
{% endblock %}
#myproject/urls.py (view complete file contents)
url(r'^settings/password/$', auth_views.PasswordChangeView.as_view(template_name='password_change.html'),
name='password_change'),
url(r'^settings/password/done/$', auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'),
name='password_change_done'), #myproject/settings.py
LOGIN_URL = 'login' <!--templates/password_change.html-->
{% extends 'base.html' %}
{% block title %}Change password{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item active">Change password</li>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-lg-6 col-md-8 col-sm-10">
<form method="post" novalidate>
{% csrf_token %}
{% include 'includes/form.html' %}
<button type="submit" class="btn btn-success">Change password</button>
</form>
</div>
</div>
{% endblock %}
<!--templates/password_change_done.html-->
{% extends 'base.html' %}
{% block title %}Change password successful{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item"><a href="{% url 'password_change' %}">Change password</a></li>
<li class="breadcrumb-item active">Success</li>
{% endblock %}
{% block content %}
<div class="alert alert-success" role="alert">
<strong>Success!</strong> Your password has been changed!
</div>
<a href="{% url 'home' %}" class="btn btn-secondary">Return to home page</a>
{% endblock %}

Django入门与实践-第16章:用户登录(完结)的更多相关文章

  1. Django入门与实践-第15章:用户注销(完结)

    # myproject/settings.py LOGOUT_REDIRECT_URL = 'home' http://127.0.0.1:8000/logout/ # myproject/urls. ...

  2. Django入门与实践-第18章:访问已登录用户(完结)

    http://127.0.0.1:8000/boards/1/topics/1/ #boards/views.py @login_required def new_topic(request, pk) ...

  3. Django入门与实践-第26章:个性化工具(完结)

    http://127.0.0.1:8000/boards/1/topics/62/reply/ 我觉得只添加内置的个性化(humanize)包就会很不错. 它包含一组为数据添加“人性化(human t ...

  4. Django入门与实践-第13章:表单处理(完结)

    http://127.0.0.1:8000/boards/1/ http://127.0.0.1:8000/boards/2/ http://127.0.0.1:8000/boards/3/ http ...

  5. Django入门与实践-第25章:Markdown 支持(完结)

    http://127.0.0.1:8000/boards/1/topics/102/reply/ 让我们在文本区域添加 Markdown 支持来改善用户体验. 你会看到要实现这个功能非常简单. 首先, ...

  6. Django入门与实践-第22章:基于类的视图

    http://127.0.0.1:8000/boards/1/topics/2/posts/2/edit/ http://127.0.0.1:8000/ #boards/views.py from d ...

  7. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

  8. Django入门与实践-第14章:用户注册(完结)

    http://127.0.0.1:8000/signup/ django-admin startapp accounts INSTALLED_APPS = [ 'accounts', ] # mypr ...

  9. Django入门与实践-第12章:复用模板(完结)

    http://127.0.0.1:8000/http://127.0.0.1:8000/boards/1/http://127.0.0.1:8000/boards/2/http://127.0.0.1 ...

随机推荐

  1. cb6xe7代码提示风格变化

  2. android中配置文件property的用途以及使用<转>

    1.首先在源代码根目录(src下)下创建一个名为netconfig.properties的文件(也可以在其他目录下). 2.打开netconfig.properties文件,在该文件中添加下列代码. ...

  3. request.getParameterMap()获得Map中的数据

    今天使用request.getParameterMap()获得Map中的数据时,使用        Map map=request.getParameterMap();               i ...

  4. ArcGIS案例学习笔记1_1

    ArcGIS案例学习笔记1_1 联系方式:谢老师,135_4855_4328, xiexiaokui#qq.com 时间:第一天上午 准备 0.U盘复制ArcGIS培训*** 1.练习数据不要放到桌面 ...

  5. 吴裕雄 数据挖掘与分析案例实战(15)——DBSCAN与层次聚类分析

    # 导入第三方模块import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsfr ...

  6. linux kernel 的配置及编译

    1. 执行make menuconfig 配置内核 2. 执行make zImage 编译内核 3. 执行make modules 编译模块 4. 内核源代码的配置及编译系统 Makefile Kco ...

  7. 记一次spring-session登录后失效的问题

    用户登录后,可以进入页面,但ajax请求或跳转其他页面时,会被当做匿名用户,即没有登录.查看session数据库,发现多出两条session,一条为正常数据,里面有对应的用户名:另一条为异常的数据,没 ...

  8. go语言中通过http访问需要认证的api

    func main() { //生成client 参数为默认 client := &http.Client{} //生成要访问的url url := "https://api.XXX ...

  9. 第八章 高级搜索树 (xa1)红黑树:动机

  10. 数字与字符串之间的转换以及%f与%lf的输入输出用法区别

    1.C++字符串与C字符串的转换: (1)string --> char * string str("OK"); strcpy(p,str.c_str());//p是char ...