Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm tyring to override the url to call specific methods for different types of urls. Code below

Resources.py

class LCUserResource(ModelResource):
    class Meta:
        queryset = LCUser.objects.all()
        resource_name= 'lcuser'
        authorization = Authorization()
def override_urls(self):
    return [
        url(r'^register/'%
            (self._meta.resource_name, trailing_slash()), self.wrap_view('register_user'), name="api_register_user"),

urls.py

v1_api = Api(api_name='v1')
v1_api.register(LCUserResource())
urlpatterns = [
    url(r'^api/', include(v1_api.urls)),

I'm trying to access the api via http://localhost:8000/api/v1/lcuser/register/

But i'm getting the error global name urls is not defined.

I tried importing from django.conf.urls.defaults import *

Then I get No module named defaults

Thanks but I'm getting a not all arguments formatted error. I'm using Django 1.8 with tastypie 0.12.2 I'm wondering if my syntax is wrong on my resources.py override url – Dhanushka Amarakoon Jan 15, 2016 at 4:58

The django.conf.urls.default was removed in django 1.6 as stated in the depreciation notes:

django.conf.urls.defaults will be removed. The functions include(), patterns() and url() plus handler404, handler500, are now available through django.conf.urls.

So, it seems like the documentation for tastypie hasn't been updated for django 1.8; you can fix the import error by fixing your import as mentioned in the release note:

from django.conf.urls import url

This will solve one issue - your next problem is here:

r'^register/'% (self._meta.resource_name, trailing_slash())

Not sure you what you are trying to do here as you are parsing a string (with %) but there are no variables to substitute. It is the same problem as this:

>>> a = 'world'
>>> 'hello' % (a,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

You probably want the following:

r"^register/(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash())
                Thanks. I changed the url to ` url(r"^register/", self.wrap_view('register_user'), name="api_register_user"),` and am trying to call it via curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"cellphone": "cell", "password": "pass", "email": "email"}' http://localhost:8000/api/v1/lcuser/register/ But I get the error HTTP/1.0 501 NOT IMPLEMENTED
– Dhanushka Amarakoon
                Jan 18, 2016 at 4:08
                My bad. The url was wrong it should have been url(r"^(?P<resource_name>%s)/register%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('register_user'), name="api_register_user"),
– Dhanushka Amarakoon
                Jan 18, 2016 at 5:04
                This is  getting out of topic but how do I access request Data? request.data['email'] returns an error
– Dhanushka Amarakoon
                Jan 18, 2016 at 5:20
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.