我正在尝试创建和验证与github帐户到DRF.为什么我得到404错误,无法进入github验证页面?我提供了代码摘录.如果需要任何信息或您有任何疑问,请让我知道.
这里是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')
这里是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'
我的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
和一个非常简单的模板oauth.html
<a href="{% url "social:begin" "github-oauth2" %}">Github</a>
页面未找到(404)页面未找到GET请求URL:http://127.0.0.1:8000/login/github-oauth2/提出人:social_django.views.auth使用books.urls中定义的URLconf,Django尝试了以下URL模式,顺序如下:
admin/ login/str:backend/ [name ='begin']当前路径login/github-oauth2/与最后一个匹配。
你看到这个错误是因为你的Django设置文件中有DEBUG = True。将其更改为False,Django将显示一个标准的404页面。
1条答案
按热度按时间rdlzhqv91#
我在github上的授权也遇到了同样的问题。授权第一次只工作了一次,然后总是显示页面找不到(404)。这是我的问题,因为我已经登录了,oauth jast把我重定向到主页。所以,为了解决这个问题,我去了:
https://github.com/settings/developers
打开我的应用程序,并“撤销所有用户令牌”。