我正在尝试一个使用tastepie 和django-registration 来注册api 的示例。当我执行 test_register.py 测试文件时,我得到一个错误“'WSGIRequest' object has no attribute 'data'”。我认为这可能与捆绑的概念有关。任何帮助使此方案正常工作表示赞赏。
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' :'s3@gmail.com', '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
网址.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' 对象不可调用"