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路径 ], }, }, ]