• Django开发笔记三


    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路径
                ],
            },
        },
    ]
    
  • 相关阅读:
    SQLServer: 解决“错误15023:当前数据库中已存在用户或角色
    DEV界面皮肤
    模拟业务最小测试用例
    POJ 2503 Babelfish(map)
    POJ 2001 Shortest Prefixes
    洛谷 P2672 推销员
    POJ 2104 K-th Number && 洛谷 P3834 【模板】可持久化线段树 1(主席树)
    洛谷 P1589 泥泞路
    HDU 6183 Color it(动态开点线段树)
    POJ 2482 Stars in Your Window
  • 原文地址:https://www.cnblogs.com/lanqie/p/7657560.html
Copyright © 2020-2023  润新知