django.core.exceptions.ImproperlyConfigured。包含的URLconf似乎没有任何模式

时间:2018-01-18 05:11:50

标签: python django django-rest-framework

我正在尝试使用django rest框架进行简单的crud演示项目API移动并获得以下错误

  

django.core.exceptions.ImproperlyConfigured:包含的URLconf''似乎没有任何模式。如果您在文件中看到有效模式,则问题可能是由循环导入引起的。

项目名称: crud_demo

应用名称:crud_operations

crud_demo / urls.py

from django.conf.urls import include, url
from django.contrib import admin

admin.autodiscover()


urlpatterns= [
    url(r'^admin/', admin.site.urls),
    url(r'^crud_operations/', include('crud_operations.urls')),
]

crud_operations / models.py

  from django.db import models

# Create your models here.

class Member(models.Model):
    firstname = models.CharField(max_length=40)
    lastname = models.CharField(max_length=40)

crud_operations / serializers.py

from crud_operations.models import Member
from rest_framework import serializers

class MemberSerializer(serializers.ModelSerializer):
    class Meta:
        model = Member
        fields = ('firstname', 'lastname')

crud_operations / urls.py

from crud_operations import views

urlpatterns = [
     url(r'^models/$', include(views.model_list)),
     url(r'^models/(?P<pk>[0-9]+)$', include(views.model_detail)),
]

crud_operations / views.py

from django.shortcuts import render, redirect
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from .models import Member
from crud_operations.serializers import MemberSerializer

@api_view(['GET', 'POST'])
def model_list(request):
    if request == 'GET':
        member = Member.objects.all()
        serializer = MemberSerializer(member)
        return Response(serializer.data)
    elif request == 'POST':
        serializer = MemberSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    return Response({'key': request}, status=status.HTTP_200_OK)


@api_view(['GET', 'PUT', 'DELETE'])
def model_detail(request, pk):
    try:
        member = Member.objects.get(pk=pk)
    except Member.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request == 'GET':
        serializer = MemberSerializer(member)
        return Response(serializer.data)

    elif request == 'PUT':
        serializer = MemberSerializer(member, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        member.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

    return Response({'key': 'value'}, status=status.HTTP_200_OK)

编辑Rohan Varma答案 更改后的crud_demo / urls.py

from crud_operations import views

urlpatterns = [
     url(r'^models/$', views.model_list),
     url(r'^models/(?P<pk>[0-9]+)$', views.model_detail),
]

获得以下错误: Error 1

EDIT for Exprator Answer 更改了crud_demo / urls.py之后     来自crud_operations导入视图

    urlpatterns = [
          path(r'^models/$', views.model_list),
          path(r'^models/(?P<pk>[0-9]+)$', views.model_detail),
    ]

获得以下错误: Error2

2 个答案:

答案 0 :(得分:1)

如果您使用的是django 2.0.1,您应该知道django已经从django 2.0更改了其urlpatterns 在您的网址中使用以下代码,并删除不需要的admin.autodiscover()

from django.urls import path

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    path('articles/<yyyy:year>/', views.year_archive),
    ...
]

更改此行

path(r'^models/(?P<pk>[0-9]+)$', views.model_detail),

path('models/<int:pk>/', views.model_detail),

path(r'^models/$', views.model_list),

path('models/', views.model_list),

答案 1 :(得分:0)

问题出在crud_operations / urls.py中,因为您正在使用include尝试将视图链接到网址。 include用于将网址链接到另一个url.py配置集。要将视图函数链接到URL,只需在其中键入函数的名称:

from crud_operations import views

urlpatterns = [
     url(r'^models/$', views.model_list),
     url(r'^models/(?P<pk>[0-9]+)$', views.model_detail),
]
祝你好运!