2013-07-18 39 views
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”

+0

只要使用此:http://stackoverflow.com/questions/11770501/how-can-i-login-to-django-using-tastypie –

回答

1

我尝试使用RegistrationProfile代替。有效。 (请参阅django-registration/tests/models.py中的示例)。这将是像

from registration.models import RegistrationProfile 
new_user = RegistrationProfile.objects.create_inactive_user(
         username=username, 
         password=password, 
         email=email, site=Site.objects.get_current(), 
         send_email=True)