python利用django实现简单的登录和注册,并利用session实现了链接数据库

利用session实现与数据库链接,登录模块(在views.py)

def login(request):
    # return HttpResponseRedirect('/')

    # 判断是否post方式,如果是则进行下面的表单处理
    if request.method == 'POST':
        rs = Users.objects.filter(email=request.POST.get('email'), #django的filter方法是从数据库的取得匹配的结果,返回一个对象列表,如果记录不存在的话,它会返回[]。 比如我数据库里有一条记录,记录的name的值是Python的话,我用student = Student.objects.filter(name='老王python') 它返回的student是一个对象的列表,可以看的出来student[0]和上面的get方式返回的student的结果是一样的。
        pwd=request.POST.get('pwd')).first()
        request.session['nicheng'] = rs.nicheng
        request.session['pwd'] = rs.pwd
        # return HttpResponseRedirect('/')
        if (rs == None):
            return HttpResponse('<script>alert("请注册后在登陆");location.href="/";</script>')
        else:
            return render(request, 'home.html')

            # nicheng_val = request.POST.get('nicheng')
            # email_val = request.POST.get('email')
            # pass_val = request.POST.get('pass')
            #
            # if (nicheng_val != "" and pass_val != ''and email_val != ''):
            #
            #     user = authenticate(name=nicheng_val, email=email_val, password=pass_val)  # 用户验证
            #
            #     if (user != None):
            #
            #         # return HttpResponseRedirect('loginfailed.html')
            #         # return HttpResponseRedirect('/app1/loginfailed/')
            #         return HttpResponse('<script>alert("登陆成功");location.href="/";</script>')

    #
    # request.session['nicheng'] = nicheng
    # request.session['pwd'] = login.pwd
    # return HttpResponseRedirect('/')
    #
    #
    def home(request):
        ...
        if 'nichenng' in request.session:
            nicheng = request.session['nicheng']
            pwd = request.session['pwd']
            return render('home.html', {'nicheng': nicheng, 'pwd': pwd})
        else:
            return render('home.html')    

  注册模块(views.py下)

def register(request):

    if request.method == 'POST':

        email = request.POST.get('email')
        pwd = request.POST.get('pwd')
        nicheng = request.POST.get('nicheng')

        try:

            Users.objects.create(email=email, pwd=pwd, nicheng=nicheng)  # 创建一个对象实例 成功注册
            return redirect('/')

        except Exception as err:

            errStr = err.args[1]

            if 'emailuniq' in errStr:
                return HttpResponse('<script>alert("用户名重复");location.href="/";</script>')
            elif 'nichenguniq' in errStr:
                return HttpResponse('<script>alert("昵称重复");location.href="/";</script>')
                # return  render(request,'zhucesuccess.html')

    else:
        # return render(request, 'zhucefailed.html')
        return redirect('/')

  register.html下

<form method="post" action="/novelnet1/register">

    <input type="text" name="email" class="form-control" placeholder="请输入昵称"><br />
    <input type="password" name="pwd" class="form-control" placeholder="请输入邮箱"><br />
    <input type="text" name="nicheng" class="form-control" placeholder="请输入密码"><br />

    <input type="submit" value="注册" class="btn btn-primary"/>
    {% csrf_token %}
</form>

  login.html下同理

<form method="post" action="/novelnet1/login1">

    <input type="text" name="email" class="form-control" placeholder="请输入昵称"><br />
    <input type="password" name="pwd" class="form-control" placeholder="请输入邮箱"><br />
    <input type="text" name="nicheng" class="form-control" placeholder="请输入密码"><br />

    <input type="submit" value="登录" class="btn btn-primary"/>
    {% csrf_token %}
</form>

  urls下

# from django.conf.urls import url
# from django.contrib.auth import views as auth_views
# from django.views.generic.base import TemplateView
# from novelnet1 import views as core_views
# from . import views
#
# # app_name = 'app1'
#
# urlpatterns = [
#
#     # start login
#     url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
#
#     url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
#     url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
#     # end login
#
#     # start register
#     url(r'^register/$', core_views.register, name='register'),
#     # end register
#
#     url(r'^testform/$', views.testform, name='testform'),
#
# ]
#
from django.conf.urls import url
from django.conf.urls import include
from . import views

urlpatterns = [

    url(r'^$', views.index, name='index'), #首页
    url(r'^register', views.register, name='register'),
    url(r'^login1',views.login,name='login'),

    # url(r'^testform/$', views.testform, name='testform'),


]

  home.html下的登录注册

 <div class="tmp">
        <button data-toggle="modal" data-target="#myModal1">登录</button>
        <button data-toggle="modal" data-target="#myModal">注册</button>
    </div>
    <!-- 模态框(Modal) -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">注册</h4>
                </div>
                <div class="modal-body">
                    {% include "users/register.html" %}
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">登录</h4>
                </div>
                <div class="modal-body">
                    {% include "users/login.html" %}
                </div>
            </div>
        </div>
    </div>

  

posted @ 2017-11-07 17:56  都是他舅  阅读(1230)  评论(0编辑  收藏  举报