跳转至

扩展


配置 apps

from django.apps import AppConfig

class 应用名Config(AppConfig):     # 首字母大写
    name = '应用名'
    verbose_name = 'app显示名称'

init.py

default_app_config = '应用.apps.应用Config'    # 第二个应用名首字母大写

数据添加时的时间

models.py

from datetime import datetime   

字段 = models.DateTimeField(default=datetime.now)     # 数据添加的时间

登陆

from django.contrib.auth import authenticate,login
user_name = request.POST.get('username')            # 用户
pass_word = request.POST.get('password')            # 密码
user = authenticate(username=user_name,password=pass_word)  # 正确的话user是对象否则是None
if user:
    login(request,user)                 # 登陆
    return render(request, 'index1.html')
else:
    return render(request, 'login.html')

登陆定义

settings.py

AUTHENTICATION_BACKENDS = (
    '目录.views.CustomBackend',
)

views.py

from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from 目录.models import              # 导入models

class CustomBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = UserProfile.objects.get(Q(username=username)|Q(email=username))#判断账号或邮箱
            if user.check_password(password):                       # 判断密码
                return user                             # 返回对象
        except Exception as e:
            return None

退出登陆

views.py

from django.contrib.auth import logout
def user_logout(request):
    logout(request)

models的user继承

settings.py

AUTH_USER_MODEL = '目录.UserProfile'

models.py

from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
    字段

用户上传文件

urls.py

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

继承判断是否登陆

mixin_utils.py 新建

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator


class LoginRequiredMixin(object):

    @method_decorator(login_required(login_url='/login/'))
    def dispatch(self,request,*args,**kwargs):
        return super(LoginRequiredMixin,self).dispatch(request,*args,**kwargs)

from utils.mixin_utils import LoginRequiredMixin 在用的页面导入

class 名称View(LoginRequiredMixin,View): 继承在需要的类里


404页面


网站地图

https://www.jianshu.com/p/fbec5c355fba


部署

https://blog.csdn.net/u014442377/article/details/80901368

ssl

https://blog.csdn.net/duyusean/article/details/79348613

https://www.cnblogs.com/renew/p/7910621.html

网站压缩

nginx.conf 文件 http{} 里

gzip  on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php 
image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

全局变量

from django.conf import settings


国际化

http://blog.csdn.net/scissors0707/article/details/79042458 http://mlocati.github.io/articles/gettext-iconv-windows.html

setting.py

USE_I18N = True
USE_L10N = True
USE_TZ = True

LANGUAGES = (
    ('en', ('English')),
    ('zh-hans', ('中文简体')),
    ('zh-hant', ('中文繁體')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
MIDDLEWARE 
'django.middleware.locale.LocaleMiddleware',

templates

{% load i18n %}
<title>{% trans "This is the title." %}</title>

命令

python manage.py makemessages -l zh_hans

django-admin compilemessages


页面分享

https://github.com/overtrue/share.js/


富文本编辑器

simditor是一个简介并强大的编辑器

官网: http://simditor.tower.im/demo.html

项目: https://github.com/mycolorway/simditor

扩展表情包: https://github.com/mycolorway/simditor-emoji


数据库转移

博客: https://blog.csdn.net/u013465115/article/details/107971569