简体   繁体   中英

Django rest framework, tokken authentification, but not for the creation of users

I've been making a simple project with DJango and his rest framework, I want my users to authenticate with tokkens, the problem is that when I try to register users I get this message: "Authentication credentials were not provided".

Thi is a problem, a first time user should be able to register without authentication, any idea how can I make my users registrate witout tokkens?.

Here are my User serializer:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password', )

The user views:

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

The urls:

router = routers.DefaultRouter()
router.register(r'devices', views.DeviceViewSet)
router.register(r'users', views.UserViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', cosa.obtain_auth_token),
]

The problem is that when I tried to create a User with this command:

http POST http://127.0.0.1:800/users/ username="Foo" password"admin123456" email="foo@gmail.com"

I get this message: "Authentication credentials were not provided".

Any idea how to fix it?

from rest_framework.permissions import AllowAny

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    permission_classes = (AllowAny, )

    queryset = User.objects.all()
    serializer_class = UserSerializer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM