models.pyDjango的添加()到底需要2個參數(3給出)
#!/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
views.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
url.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/add -o test.html
您可能想要修復代碼示例的縮進。看起來你在幾個點上輸了一個等級。 – 2013-02-17 22:59:46
如果這裏有縮進問題,請忽略它。我複製並粘貼代碼在這裏,即時通訊新的將代碼放在stackoverflow,所以它可能看起來不好。但我的實際代碼的縮進很好。我之前有縮進錯誤,我修正了它們,現在是它。 – Jeff 2013-02-18 00:10:10
如果你的代碼縮進很差,我們應該怎麼知道它應該是什麼樣子? – 2013-02-18 01:12:05