1

我已经导入了所有的 restframework 包,但我不明白为什么会出现这个奇怪的错误

from django.conf.urls import *
from django.contrib import admin
from django.views.generic.base import TemplateView
from .views import Create,Home,signup,Search,QuestionViewSet
from django.urls import reverse
from .models import Question
from django.contrib.auth import views as auth_views
from rest_framework import  routers
router=routers.DefaultRouter()
router.register(prefix='question1',viewset=QuestionViewSet)
app_name='main'

urlpatterns = [
    # url(r'^/',views.home,name='home'),
    url(r'^home/',Home,name='home'),
    url(r'^ques/',Create.as_view(success_url="/index/home/"),name='ques'),
    url(r'^signup/',signup,name='signup'),
    url(r'^logout/$', auth_views.logout,name='logout'),
    url(r'^search/',Search,name='search'),
    url(r'^api/', include('router.urls'))


    # CreateView.as_view(model=myModel, success_url=reverse('success-url'))

]

这是我面临的问题

ModuleNotFoundError: No module named 'router'

提前感谢任何形式的帮助

4

1 回答 1

2

方法的第一个参数include可以是模块名称或模式列表。既然router.urls是模式列表,它应该是

url(r'^api/', include(router.urls))

代替

url(r'^api/', include('router.urls')) 

所以你需要删除''标志。

于 2018-05-02T16:35:08.960 回答