1
我正在嘗試使用tastypie和django註冊有api註冊的示例。當我執行test_register.py測試文件時,出現錯誤「'WSGIRequest'對象沒有屬性'data'」。 我認爲這可能與捆綁的概念有關。任何幫助獲得這個場景的工作表示讚賞。爲django註冊啓用tastypie api
api.py
from registration.views import register
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from tastypie.http import HttpUnauthorized, HttpForbidden
from django.conf.urls.defaults import url
from tastypie.utils import trailing_slash
from registration.models import RegistrationManager
class RegisterUserResource(ModelResource):
class Meta:
allowed_methods = ['post']
# authentication = Authentication()
# authorization = Authorization()
include_resource_uri = 'register'
fields = ['username','password']
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/register%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('register'), name="api_register"),
]
def register(self, bundle, request=None, **kwargs):
username, password = bundle.data['username'], bundle.data['password']
print "reached register"
try:
bundle.obj = RegistrationManager.create_inactive_user(username, username, password)
except IntegrityError:
raise BadRequest('That username already exists')
return bundle
test_register.py
import requests
import json
from urllib2 import urlopen
import datetime
import simplejson
url = 'http://127.0.0.1:8000/apireg/registeruser/register/'
data = {'username' :'[email protected]', 'password' : 'newpass'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
print json.dumps(data)
r = requests.post(url, data=json.dumps(data), headers=headers)
print r.content
urls.py
from userdetails.api import RegisterUserResource
register_userresource = RegisterUserResource()
admin.autodiscover()
urlpatterns = patterns('',
...
(r'^apireg/', include(register_userresource.urls)),
)
更新
我對註冊方法進行了一些更改而不使用包,現在基本功能正常工作。這會創建一個非活動用戶。
def register(self,request, **kwargs):
data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
# try:
# userreg = RegistrationManager
RegistrationManager().create_inactive_user(username, username, password,'',send_email=False)
即使這個工程,我仍然得到一個錯誤,我試圖解決。 「error_message」:「'NoneType'object is callable」
只要使用此:http://stackoverflow.com/questions/11770501/how-can-i-login-to-django-using-tastypie –