Django开发笔记一

Django开发笔记二

Django开发笔记三

Django开发笔记四

Django开发笔记五

Django开发笔记六

1、基于类的方式重写登录:views.py:

from django.views.generic.base import View

class LoginView(View):
def get(self,request):
return render(request, "test/login.html", {}) def post(self,request):
user_name = request.POST.get("username", "")
pass_word = request.POST.get("password", "")
user = authenticate(username=user_name, password=pass_word) if user is not None:
dj_login(request, user)
return render(request, "test/index.html", {})
else:
return render(request, "test/login.html", {"msg": "用户名或密码错误"})

users.py:

url(r'^login/$', LoginView.as_view(), name="login"),

2、用python变量描述文件路径,方便统一修改

                        <a style="color:white" class="fr registerbtn" href="{% url 'register' %}">注册</a>
<a style="color:white" class="fr loginbtn" href="/login/">登录</a>
{% load staticfiles %}
......
<link rel="stylesheet" type="text/css" href="/static/css/test/reset.css">
<link rel="stylesheet" type="text/css" href="/static/css/test/animate.css">
<link rel="stylesheet" type="text/css" href="{% static 'css/test/style.css'%}">

3、验证码

django-simple-captcha文档:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation

urls.py:

urlpatterns = [
url(r'^captcha/', include('captcha.urls')), ]

settings.py:

INSTALLED_APPS = [
'captcha', ]

forms.py:

class RegisterForms(forms.Form):
email = forms.CharField(required=True)
password = forms.CharField(required=True, min_length=6)
captcha = CaptchaField()

views.py:

class RegisterView(View):
def get(self, request):
register_forms = RegisterForms()
return render(request, "test/register.html", {"register_forms": register_forms}) def post(self, request):
register_forms = RegisterForms(request.POST)
if register_forms.is_valid():
email = request.POST.get("email", "")
pass_word = request.POST.get("password", "")
if UserProfile.objects.filter(email=email):
return render(request, "test/register.html", {"msg": "邮箱已被注册"})
else:
user_profile = UserProfile()
user_profile.username = email
user_profile.email = email
user_profile.password = make_password(pass_word)
user_profile.save()
return render(request, "test/login.html", {}) else:
return render(request, "test/register.html", {"register_forms": register_forms})

register.html:

                 <form id="email_register_form" method="post" action="/register/" autocomplete="off">
<div class="form-group marb20 ">
<label>邮     箱</label>
<input type="text" id="id_email" name="email" value="" placeholder="请输入您的邮箱地址" />
</div>
<div class="form-group marb8 ">
<label>密     码</label>
<input type="password" id="id_password" name="password" value="" placeholder="请输入6-20位非中文字符密码" />
</div>
<div class="form-group marb8 captcha1 ">
<label>验 证 码</label> {{ register_forms.captcha }}
</div>
<div class="error btns" id="jsEmailTips"></div>
<div class="auto-box marb8">
</div>
<input class="btn btn-green" id="jsEmailRegBtn" type="submit" value="注册并登录" />
{% for key ,error in register_forms.errors.items %}{{ error }}{% endfor %}{{ msg }}
{% csrf_token %}
</form>

4、文件上传:

settings.py

MEDIA_URL = '/static/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images/')

models.py:

image = models.ImageField(upload_to="course/%y/%m", verbose_name=u"封面图", max_length=100)

html显示:

          {% for video in video_list%}
<div class="item">
<a href="">
<img src="{{ MEDIA_URL }}{{ video.image }}">
<p class="title">{{ video.name }}</p>
</a>
</div>
{% endfor %}

需要在settings.py:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
.......
'django.template.context_processors.media', # 配置html页面获取MEDIA_URL路径
],
},
},
]

Django开发笔记三的更多相关文章

  1. Django开发笔记六

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.登录功能完善 登录成功应该是重定向到首页,而不是转发 ...

  2. Django开发笔记五

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.页面继承 定义base.html: <!DOC ...

  3. Django开发笔记四

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.邮箱激活 users app下,models.py: ...

  4. Django开发笔记二

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.xadmin添加主题.修改标题页脚和收起左侧菜单 # ...

  5. Django开发笔记一

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...

  6. Django开发笔记(一)

    Django开发笔记(一) 标签(空格分隔): Django Python 1. 创建并运行Django项目 创建开发环境 安装Django pip install django==version 执 ...

  7. Django开发笔记之数据库的设计

    后台采用Django开发,可以体会到开发的便利之处,对于一个项目来说,首先最重要的是数据库的设计,那么在Django下数据库设计主要是如下步骤: 1,需求分析,这点子不用多说,而我也深刻体会到了没有原 ...

  8. Django开发笔记

    django基础之视图 1.在Django中网页前端的页面和其他内容都是由视图来传递的(视图对web请求进行回应)Django通过检查请求的URL(准确地说,是URL里域名之后的那部分)来选择使用哪个 ...

  9. RBL开发笔记三

    2014-08-26 20:06:24 今天就是在开发这个EPOLL来处理网络事件 封装较为健壮的EPOLL模型来处理基本的网络IO 1) 超时这个主题先没有弄 在开发EPOLL包括select/po ...

随机推荐

  1. hdu 3727 Jewel (可持久化线段树+bit)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3727 题意: 对一段序列进行四种操作: Insert x :在序列尾部插入一个x: Query_1 s ...

  2. Codeforces Round #428 (Div. 2)A,B,C

    A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. MT【40】一道联赛二试题

    让我通过这道题来演示如何利用切比雪夫多项式的内功心法: 评:如此大道至简,当年为之叫绝的精彩的做法

  4. NOI 笔试题库(我背不住的部分)

    吐槽 为什么C++选手要会编译Pascall啊!为什么Emacs选手要会使用Vim啊! Linux 中为文件改名使用的命令是:mv 在Linux 中删除当前目录下的test 目录的命令是:rm -r ...

  5. 洛谷 P2764 最小路径覆盖问题 解题报告

    P2764 最小路径覆盖问题 问题描述: 给定有向图\(G=(V,E)\).设\(P\) 是\(G\) 的一个简单路(顶点不相交)的集合.如果\(V\) 中每个顶点恰好在\(P\) 的一条路上,则称\ ...

  6. Spark 集成开发

    WordCount.py # coding:utf-8 from pyspark import SparkContext from pyspark import SparkConf def SetLo ...

  7. [IOI2018] seats 排座位

    [IOI2018] seats 排座位 IOI2018题解 压缩状态思想很不错的 每次把原来的贡献减掉,新来的再加上 最多涉及10个点 注意: 1.去重 2.下标从0开始 3.线段树初始的最小值个数都 ...

  8. java NIO 直接与非直接缓冲区

    ByteBuffer有两个创建缓冲区的方法:static ByteBuffer allocate(int capacity)static ByteBuffer allocateDirect(int c ...

  9. 移动UI布局设计原则(一)

    学习笔记1 Learning notes one 移动UI布局设计的布局原则 Layout Principles of Mobile UI Layout Design 移动UI视觉交互设计法则 Des ...

  10. Apache的ProxyPass简单使用

    转: Apache的ProxyPass简单使用 置顶 2017年08月14日 18:54:33 师太,老衲把持不住了 阅读数:11164   http://mtnt2008.iteye.com/blo ...