Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

Django Rest Framework and Heroku - Error 401

I just deployed my website on Heroku, an integral part of the functionality of the site is to receive post requests from another website of mine. I'm using DRF's TokenAuthentication for my authentication. In my local environment everything works perfectly, but the same cannot be said for the live website. Whenever I send a POST request I receive a 401 error, the new token is added to the request's header and the data is exactly the same as the data used in the local environment. Any help would be gladly appreciated.

views.py

class PetCreateView(CreateAPIView):
    serializer_class = PetSerializer
    permission_classes = [IsAuthenticated, ]

    def perform_create(self, serializer):
        if Breed.objects.filter(user_id=self.request.POST.get('breed_name', None)).exists():
            pass
        else:
            Breed.objects.create( breed_name=self.request.POST.get('breed_name', None),
                                  size=self.request.POST.get('size', None),
                                  temperament=self.request.POST.get('temperament', None),
                                  energy=self.request.POST.get('energy', None),
                                  hypoallergenic=self.request.POST.get('hypoallergenic', None))

        breed = Breed.objects.get(breed_name=self.request.POST.get('breed_name', None))
        # Assigns a groomer the created Pet
        groomer = assign_groomer(breed)
        serializer.save(groomer=groomer, breed=breed)

        SpaReport.objects.create(breed=breed, groomer=groomer)

 

settings.py

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'bootstrap3',
    'captcha',
    'django_celery_beat',
    'django_celery_results',
    'mathfilters',
    'storages',
    'account',
    'sales',
    'scrty',
    'analytics'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'pettracker.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'scrty.context_processors.from_settings',
            ],
        },
    },
]

WSGI_APPLICATION = 'pettracker.wsgi.application'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...