Django表单和数据库

时间:2017-08-20 06:26:28

标签: python django django-models django-forms django-views

我现在正在工作宽度Django。但我对此没有意义。

我想从表单中获取id和密码,并检查表单中的密码是否正确,以便与数据库的密码进行比较。

以下是我的代码。

请帮帮我。

models.py

from django.db import models

class Doctor(models.Model):
    doctor_id = models.CharField(max_length=16, primary_key=True)
    clinic_id = models.ForeignKey(Clinic)
    doctor_email = models.CharField(max_length=64)
    doctor_password = models.CharField(max_length=32)
    doctor_name = models.CharField(max_length=32)
    create_date = models.DateTimeField(auto_now_add=True)
    modify_date = models.DateTimeField(auto_now=True)

forms.py

from django import forms
from .models import Doctor

class LoginForm(forms.Form):
class Meta:
    model = Doctor
    fields = ('doctor_id', 'doctor_password',)

views.py

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .forms import LoginForm
from .models import Doctor

@ensure_csrf_cookie
def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            _id = form.cleaned_data['doctor_id']
            _password = form.cleaned_data['doctor_password']
            b = Doctor.objects.all().filter(doctor_id=_id)

            if _password is doctor_password:
                login(request, user)
                return HttpResponse('Authenticated successfully')
            else:
                return HttpResponse('Disabled account')
        else:
            return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'apiv1/login.html', {'form': form})

的login.html

{% extends "base.html" %}
{% load staticfiles%}

{% block title%}Title{% endblock %}

{% block remoshincss %}/static/css/style.css{% endblock %}

{% block content %}

<div class="container">
    <div align="center" class="imgtop"><img id="profile-img" class="profile-img-card" src="/static/img/remoshinlogo.png" /></div>
    <div class="card card-container">
        <p id="profile-name" class="profile-name-card"></p>
        <form class="form-signin" action="{% url 'login' %}" method="post">{% csrf_token %}
             <input type="user" id="userid" name="userid" class="form-control inputUser" placeholder="USER-ID" autofocus>
            <input type="password" id="password" name="password" class="form-control inputPassword" placeholder="PASSWORD">
            <input type="hidden" name="next" value="{{ next }}" />
            <br>
            <div align="center"><button style="width: 200px;" class="btn btn-lg btn-primary btn-block btn-signin" type="submit"><font color="#708090">Login</font></button></div>
        </form>
    </div>
</div>

{% endblock %}

1 个答案:

答案 0 :(得分:1)

导入check_password

  

来自django.contrib.auth.hashers导入check_password

检查密码

pass_ = check_password(_password, b.doctor_password)
if pass_ is False:
   return HttpResponse('Invalid login')

代码:

@ensure_csrf_cookie
def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            _id = form.cleaned_data['doctor_id']
            _password = form.cleaned_data['doctor_password']
            docter = Doctor.objects.filter(doctor_id=_id).last()

            if docter is None:
                return HttpResponse('Invalid login') 
            pass_ = check_password(_password, docter.doctor_password)
            if pass_ is False:
                return HttpResponse('Invalid login')
            return HttpResponse('Authenticated successfully')
        else:
            return HttpResponse('Invalid login')
    else:
        form = LoginForm()
    return render(request, 'apiv1/login.html', {'form': form})