I V
I V

Reputation: 55

Django Rest Framework: backend not found

I'm trying to create and authentication with github account to DRF. why i get 404 error and can't get to github authentication page? I provided the code extracts. If any information is required or you have any questions please let me know.

Here's the views.py

class BookViewSet(ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BooksSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
    permission_classes = [IsAuthenticated]
    filterset_fields = ['price']
    search_fields = ['name', 'author_name']
    ordering_fields = ['price', 'author_name']

def auth(request):
    return render(request, 'oauth.html')

And here's the settings.py



# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    
    'social_django',

    'store',
]

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 = "books.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": ['templates'],
        "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",
            ],
        },
    },
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'books_db',
        'USER': 'books_user',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
        'TEST': {
            'NAME': 'test_finance',
        },
    }
}


AUTHENTICATION_BACKENDS = (
    'social_core.backends.github.GithubOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)


REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated', )
}

SOCIAL_AUTH_POSTGRES_JSONFIELD = True

SOCIAL_AUTH_GITHUB_KEY = '-some github key'
SOCIAL_AUTH_GITHUB_SECRET = 'git secret'

My urls.py


router = SimpleRouter()

router.register(r'book', BookViewSet)

urlpatterns = [
    path("admin/", admin.site.urls),
    re_path('', include('social_django.urls', namespace='social')),
    path('auth/',auth)
]

urlpatterns += router.urls

And a very simple template oauth.html

<a href="{% url "social:begin" "github-oauth2" %}">Github</a>

Page not found (404) Backend not found Request Method: GET Request URL: http://127.0.0.1:8000/login/github-oauth2/ Raised by: social_django.views.auth Using the URLconf defined in books.urls, Django tried these URL patterns, in this order:

admin/ login/str:backend/ [name='begin'] The current path, login/github-oauth2/, matched the last one.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Upvotes: 1

Views: 265

Answers (1)

Shkum
Shkum

Reputation: 681

I had same issue with authorization via github. Authorization worked only one first time and then always showed page not found (404). It was happened with me because I was already logged in and oauth jast redirect me to main page. So, to fix it I went to :

https://github.com/settings/developers

Opened my app, and "revoke all users tokens".

Upvotes: 0

Related Questions