Anaconda+django写出第一个web app(七)

时间:2022-04-24 23:10:51

今天来实现如何在页面弹出一些信息,比如注册成功后弹出注册成功的信息。这一点可以通过materialize里的Toasts来实现。

django自带的messages可以告诉我们是否注册成功,以及注册失败,或者提出警告。我们首先修改views.py,来引入messages:

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Tutorial
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login, logout, authenticate
from django.contrib import messages
# Create your views here.
def homepage(request):
return render(request=request,
template_name='main/home.html',
context={'tutorials':Tutorial.objects.all}) def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"New account created: {username}")
login(request, user)
return redirect('main:homepage')
else:
for msg in form.error_messages:
messages.error(request, f"{msg}:{form.error_messages[msg]}") return render(request=request,
template_name='main/register.html',
context={'form':form}) form = UserCreationForm
return render(request=request,
template_name='main/register.html',
context={'form':form})

如果注册成功,我们使用了messages.success来给出信息,如果失败我们使用了messages.error来给出信息。

接下来我们需要在html文件中使得这些信息可以显示,因为messages不止出现在注册时,所以我们将这些信息写在header.html中,我们还希望在注册成功后,导航栏右上角的文字发生变化,loigin变为刚注册的用户名,register变为logout,修改后header.html的内容如下:

<head>
{% load static %}
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link href="{% static 'tinymce/css/prism.css' %}" rel="stylesheet">
</head> <body>
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success'%}
<script>M.toast({html: "{{message}}", classes: 'green rounded', displayLength:2000});</script>
{% elif message.tags == 'info'%}
<script>M.toast({html: "{{message}}", classes: 'blue rounded', displayLength:2000});</script>
{% elif message.tags == 'warning'%}
<script>M.toast({html: "{{message}}", classes: 'orange rounded', displayLength:10000});</script>
{% elif message.tags == 'error'%}
<script>M.toast({html: "{{message}}", classes: 'red rounded', displayLength:10000});</script>
{% endif %}
{% endfor %}
{% endif %}
<nav>
<div class="nav-wrapper">
<a href="#" class="brand-logo">Tutorials</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="/">Home</a></li>
{% if user.is_authenticated %}
<li><a href="/account">{{ user.username }}</a></li>
<li><a href="/logout">Logout</a></li>
{% else %}
<li><a href="/login">Login</a></li>
<li><a href="/register">Register</a></li>
{% endif %}
</ul>
</div>
</nav> <div class="container">
{% block content %}
{% endblock %}
</div> </body> <script src="{% static 'tinymce/js/prism.js' %}"></script>

Anaconda+django写出第一个web app(七)

此时的header.html内容显得有些复杂和凌乱,我们可以使用include来将某些代码放入单独的html文件中来缩短header.html的内容,为此我们在templates/main文件夹下新建文件夹includes,然后新建两个文件messages.html和navbaritems.html,最终header.html、messages.html、navbaritems.html内容分别如下:

<head>
{% load static %}
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link href="{% static 'tinymce/css/prism.css' %}" rel="stylesheet">
</head> <body>
{% include 'main/includes/messages.html' %}
<nav>
<div class="nav-wrapper">
<a href="#" class="brand-logo">Tutorials</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="/">Home</a></li>
{% include 'main/includes/navbaritems.html' %}
</ul>
</div>
</nav> <div class="container">
{% block content %}
{% endblock %}
</div> </body> <script src="{% static 'tinymce/js/prism.js' %}"></script>
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success'%}
<script>M.toast({html: "{{message}}", classes: 'green rounded', displayLength:2000});</script>
{% elif message.tags == 'info'%}
<script>M.toast({html: "{{message}}", classes: 'blue rounded', displayLength:2000});</script>
{% elif message.tags == 'warning'%}
<script>M.toast({html: "{{message}}", classes: 'orange rounded', displayLength:10000});</script>
{% elif message.tags == 'error'%}
<script>M.toast({html: "{{message}}", classes: 'red rounded', displayLength:10000});</script>
{% endif %}
{% endfor %}
{% endif %}
{% if user.is_authenticated %}
<li><a href="/account">{{ user.username }}</a></li>
<li><a href="/logout">Logout</a></li>
{% else %}
<li><a href="/login">Login</a></li>
<li><a href="/register">Register</a></li>
{% endif %}

在浏览器中刷新页面,仍然正常显示。这种方法可以保证单个文件的内容不至于过长,便于后期管理。本文中刚开始提到的信息提示,没有相应的截图,这个可以在下一节添加login和logout时清楚的观察到。

参考链接:

[1] https://materializecss.com/toasts.html

[2] https://pythonprogramming.net/messages-django-tutorial/

Anaconda+django写出第一个web app(七)的更多相关文章

  1. Anaconda&plus;django写出第一个web app(一)

    在安装好Anaconda和django之后,我们就可以开始创建自己的第一个Web app,那么首先创建一个空文件夹,之后创建的文件都在这个文件夹内. 启动命令行进入此文件夹内,可以先通过如下命令查看一 ...

  2. Anaconda&plus;django写出第一个web app(十一)

    今天我们来学习给页面添加一个Sidebar,根据Sidebar跳转到相应的tutorial. 打开views.py,编辑single_slug函数: def single_slug(request, ...

  3. Anaconda&plus;django写出第一个web app(十)

    今天继续学习外键的使用. 当我们有了category.series和很多tutorials时,我们查看某个tutorial,可能需要这样的路径http://127.0.0.1:8000/categor ...

  4. Anaconda&plus;django写出第一个web app(九)

    今天来学习外键的使用,用外键来连接数据库中的两个表. 当我们的tutorials非常多的时候,目前的显示方式就会使得页面非常凌乱.我们可以考虑把这些教程分为不同的系列,页面只显示标题以及概要等信息,进 ...

  5. Anaconda&plus;django写出第一个web app(三)

    前面我们已经建立了模型Tutorial,也已经可以用Navicat Premium打开数据看查看数据,接下来我们通过建立admin账户来上传数据. 在命令行执行如下命令来创建用户: python ma ...

  6. Anaconda&plus;django写出第一个web app(二)

    今天开始建立App中的第一个Model,命名为Tutorial. Model的定义在main文件夹下的models.py中通过类进行,我们希望Tutorial这个model包含三个属性:标题.内容和发 ...

  7. Anaconda&plus;django写出第一个web app(六)

    今天学习如何写一个注册用户的界面. 上一节的导航栏中我们修改了导航栏右侧的文字为register并将路径设置为/register,内容如下: <li><a href="/r ...

  8. Anaconda&plus;django写出第一个web app(四)

    前面对Models有了一些了解,今天开始进一步了解Views,了解Views如何和Models交互以及了解模板(templates). 打开main文件夹下的views.py,重新编写homepage ...

  9. Anaconda&plus;django写出第一个web app(八)

    今天来实现网站的登入和登出功能. 首先我们需要在urls.py中添加路径,注意此处的路径和在导航栏中设置的文字路径保持一致: from django.urls import path from . i ...

随机推荐

  1. ue4 build configuration的解释

    ue4的build系统,继承并发展了3代的一如既往的复杂.. 一.每个configuration由两部份组成:[(性能)模式]+[(内容)组成] 模式有:Debug,DebugGame,Develop ...

  2. unity3d进行脚本资源打包加载

    原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...

  3. Devexpress 之gridControl双击行事件

    MouseDown事件 protected internal void gridControl1_MouseDown(object sender, MouseEventArgs e) { DevExp ...

  4. &lbrack;Angular 2&rsqb; Validation

    Define a filed should has validation: export class DemoFormSku { myForm: ControlGroup; sku: Abstract ...

  5. &lbrack;DevExpress&rsqb;图表开发工具类 ChartUtils

    /// <summary> /// 基于.NET 3.5的Chart工具类;对应的DevExpress版本:12.1.7; /// </summary> public stat ...

  6. 有趣的win8进度条

    有趣的win8进度条 刚才在安装visual studio 12,发现它的安装界面都是win8风格的,而且安装的时候有个进度条,看着挺不错,就用 jquery 实现了一下,的确挺有趣: 点击停止效果 ...

  7. 2018-01-08 学习随笔 SpirngBoot整合Mybatis进行主从数据库的动态切换&comma;以及一些数据库层面和分布式事物的解决方案

    先大概介绍一下主从数据库是什么?其实就是两个或N个数据库,一个或几个主负责写(当然也可以读),另一个或几个从只负责读.从数据库要记录主数据库的具体url以及BigLOG(二进制日志文件)的参数.原理就 ...

  8. mongodb 3&period;4 分片 一主 一副 一仲 鉴权集群部署&period;

    Docker方式部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 mongodb版本: 环境:一台虚拟机 三个configsvr 副本: 端口为 27020,27021,2 ...

  9. IDEA集成git方法

    一.IDEA集成git方法 首先idea集成git我们需要先下载一个小软件,git bash  地址:https://git-scm.com/downloads  .下载好了之后直接下一步下一步傻瓜试 ...

  10. C&plus;&plus; queue

    queuequeue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选 ...