模型.py
#!/usr/bin/env python
from django.db import models
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os
import json
import sys
import tempfile
import traceback
import re
import psycopg2
SUCCESS = 1
ERR_BAD_CREDENTIALS = -1
ERR_USER_EXISTS = -2
ERR_BAD_USERNAME = -3
ERR_BAD_PASSWORD = -4
class UsersModel(models.Model):
user = models.CharField(primary_key=True, max_length=128)
password = models.CharField(max_length=128)
count = models.IntegerField()
def login(user1, password1):
try:
con = psycopg2.connect(database='test', user='jeffrey')
cur = con.cursor()
cur.execute('SELECT user FROM UsersModel WHERE user1=user AND password1=password;')
rows = cur.fetchall()
print rows
if len(rows) == 1:
#on success update the count and return the count
cur.execute('UPDATE UsersModel SET count=count+1 WHERE user1=user AND password1=password;')
cur.execute('SELECT count FROM UsersModel WHERE user1=user AND password1=password;')
return cur.fetchone()[0]
else:
return ERR_BAD_CREDENTIALS
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
def add(user1, password1):
try:
if user1=='' or len(user1)>128:
return ERR_BAD_USERNAME
elif len(password1)>128:
return ERR_BAD_PASSWORD
con = psycopg2.connect(database='test', user='jeffrey')
cur = con.cursor()
cur.execute('SELECT user FROM login_UsersModel WHERE user1=user;')
rows = cur.fetchall()
if len(row) == 1:
return ERR_USER_EXIST
else:
cur.execute('INSERT INTO login_UsersModel VALUES (user1, password1, 1);')
cur.execute('SELECT count FROM login_UsersModel WHERE user1=user AND password1=password;')
return cur.fetchone()[0]
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
def TESTAPI_resetFixture(request):
con = psycopg2.connect(database='test', user='jeffrey')
cur = con.cursor()
cur.execute('DELETE FROM login_UsersModel')
return SUCCESS
视图.py
from django.shortcuts import render
from django.http import HttpResponse
from login.models import UsersModel
from django.utils import simplejson
import os
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def loginx(request):
data = simplejson.loads(request.body)
name = data['user']
pw = data['password']
x = UsersModel()
code = x.login(name, pw)
if code > 0:
response_data = {'errCode':1, 'count':code}
return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
else:
response_data = {'errCode':code}
return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
@csrf_exempt
def addx(request):
data = simplejson.loads(request.body)
name = data['user']
pw = data['password']
x = UsersModel()
code = x.add(name, pw)
if code > 0:
response_data = {'errCode':1, 'count':code}
return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
else:
response_data = {'errCode':code}
return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
@csrf_exempt
def TESTAPI_resetFixturex(request):
x = UsersModel()
code = x.TESTAPI_resetFixture()
response_data = {'errCode': code}
return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
@csrf_exempt
def unitTests(request):
os.system("make func_tests TEST_SERVER=localhost:8000")
return
网址.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from login.views import loginx
from login.views import addx
from login.views import TESTAPI_resetFixturex
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
#url(r'^admin/', include(admin.site.urls)),rs
url(r'^users/login', loginx),
url(r'^users/add', addx),
url(r'^TESTAPI/resetFixture', TESTAPI_resetFixturex)
#url(r'^TESTAPI/unitTests', unitTests)
)
我之前在 stackoverflow 上看到过类似的问题,但他们的解决方案无法帮助我并导致我出现其他错误。所以是的,当我只将用户和密码作为我的参数时,我不明白如何添加 3 个参数。有人能帮我吗。顺便说一句,我现在只在视图中测试添加功能:
curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"user":"test", "password":"test"}' localhost:8000/users /添加-o test.html