在我的其余API中,我已通过http://127.0.0.1:8000/admin/eatslushapp/userdetails/add/
向表userdetails添加了一些数据但是当我点击http://127.0.0.1:8000/userdetails时,我在浏览器中得到了以下结果
找不到页面(404) 请求方法:GET 要求网址:http://127.0.0.1:8000/ Django使用eatslush.urls中定义的URLconf,按以下顺序尝试了以下URL模式:
^ admin / ^ userdetails / 空路径与这些都不匹配。
您看到此错误,因为Django设置文件中的DEBUG = True。将其更改为False,Django将显示标准的404页面。
我通过观看本教程https://www.youtube.com/watch?v=ejJ-2oz4AgI
进行了尝试注意:管理员网址正常工作
以下是我的urls.py
from django.conf.urls import url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
from eatslushapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^userdetails/', views.userdetailsList.as_view()),
]
以下是settings.py
"""
Django settings for eatslush project.
Generated by 'django-admin startproject' using Django 1.11.18.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@3=r$utcg(megzsn*6v#(&2ib@78xgfmij8086x^4ec4_6k08*'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
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',
'eatslushapp',
]
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 = 'eatslush.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
WSGI_APPLICATION = 'eatslush.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
以下是我的serializer.py 从rest_framework导入序列化器 从模型导入用户详细信息
class user_detailsSerializer(serializers.ModelSerializer):
class Meta:
model = userdetails
# fields =('name',)
fields = '_all_',
以下是views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import userdetails
from .serializers import user_detailsSerializer
class userdetailsList(APIView):
def get(self, request):
user1 = userdetails.objects.all()
serializer = user_detailsSerializer(user1, many=True)
return Response(serializer.data)
def post(self):
pass
models.py
from __future__ import unicode_literals
from django.db import models
class userdetails(models.Model):
name = models.CharField(max_length=20)
profile_name = models.CharField(max_length=20)
email = models.CharField(max_length=30)
phone = models.CharField(max_length=13)
password = models.CharField(max_length=15)
def __unicode__(self):
return self.name
admin.py
from __future__ import unicode_literals
from . models import userdetails
from django.contrib import admin
admin.site.register(userdetails)
我只想获取json格式的数据