如何使用" rest_framework" Django 2的路由器

时间:2018-01-21 20:33:48

标签: python django django-rest-framework

我知道"带路由器网址模式的Django 2已经改变了#34;我无法使用路由器,因为我收到错误:

    'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

我已经编写了一个基本模型,之后我创建了一个api文件夹,我准备了文件"views.py"

from rest_framework import generics
from yogavidya.apps.articles.models import Article
from .serializers import ArticleSerializer
from rest_framework import routers, serializers, viewsets

class ArticleUpdateView(generics.RetrieveUpdateAPIView):
  lookup_field = 'pk'
  serializer_class = ArticleSerializer
  queryset = Article.objects.all()

router = routers.DefaultRouter()
router.register(r'api/articles', ArticleUpdateView, base_name='api-articles')

我有urls.py后:

from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from .views import ArticleUpdateView
from django.urls import re_path

urlpatterns = [
    re_path(r'articles/(?:page-(?P<page_number>\d+)/)?$', ArticleUpdateView.as_view(), name='article-update'),
]

现在我想将api添加到项目urls.py

from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static

from yogavidya.apps.articles import views

from .views import home,home_files, pricing
from rest_framework import routers, serializers, viewsets

urlpatterns += i18n_patterns(
    path('admin/', admin.site.urls),
    path('articles/', include('yogavidya.apps.articles.urls', namespace="articles")),
)

urlpatterns += [
    path('api/articles/', include('yogavidya.apps.articles.api.urls', namespace="api-articles")),

如何将app_name = 'api-articles'设置为全局变量并设法在urls.py中导入app_name?

0 个答案:

没有答案