Django,DRF,获取另一个应用程序的视图名称作为反向

2024-10-02 00:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用此处提供的解决方案为序列化程序中的另一个应用程序创建链接:

https://stackoverflow.com/a/45850334/12177026

我正在尝试匹配视图的名称,但每次尝试都会出现以下错误:

Reverse for 'KnownLocationView' not found. 'KnownLocationView' is not a valid view function or pattern name.

序列化程序:

class MissionSerializer(HyperlinkedModelSerializer):
    gdt = ChoiceField(choices=lazy(get_locations, tuple)())
    location = SerializerMethodField(label='Open Location')

    def get_location(self, obj):
        request = self.context.get('request')
        return request.build_absolute_uri(reverse('KnownLocationView', kwargs={'Name': obj.gdt}))

    class Meta:
        model = Mission
        fields = ('MissionName', 'UavLatitude', 'UavLongitude', 'UavElevation', 'Area',
                  'gdt', 'location')

knownAction/url.py

from django.urls import path, include
from rest_framework.routers import DefaultRouter

from .views import KnownLocationView

app_name = 'KnownLocation'
router = DefaultRouter()
router.register(r'knownlocations', KnownLocationView)

urlpatterns = [
    path('', include(router.urls)),
]

我尝试用以下任一项替换视图名称:

'knownlocations'
'KnownLocation:knownlocations'
'KnownLocation:KnownLocationView'

但是得到同样的错误

甚至尝试重新排序已安装的应用程序

api/URL.py:


urlpatterns = [
                  path('admin/', admin.site.urls),
                  path('accounts/', include('django.contrib.auth.urls')),
                  path('', include('landingpage.urls')),  # API Landing Page
                  path('', include('ThreeLocations.urls')),  # Triangulation between two Known GDTs and uav location.
                  path('', include('SecondGDT.urls')),  # Options For Second GDT Positioning.
                  path('', include('KnownLocation.urls', namespace='knownlocations')),
                  # Add Known Location To the map.
              ] + staticfiles_urlpatterns()

KnownLocation/views.py

from rest_framework.renderers import AdminRenderer
from rest_framework.viewsets import ModelViewSet

from .models import KnownLocation
from .serializers import KnownLocationSerializer


class KnownLocationView(ModelViewSet):
    """
    List of Known Locations In which we can place one of our ground positions.

    press create to add a new location.

    """
    serializer_class = KnownLocationSerializer
    queryset = KnownLocation.objects.all()

    def get_serializer_context(self):
        data = KnownLocationSerializer(self.queryset, many=True, context={'request': self.request})
        return data

    renderer_classes = [AdminRenderer]

Tags: pathfromimportselfgetincluderequestcontext
1条回答
网友
1楼 · 发布于 2024-10-02 00:23:10

我认为你应该试着改变你称之为反面的方式。 你可以在下面找到它的参考资料

注意:如果对超链接序列化程序使用名称空间,还需要确保序列化程序上的任何视图名称参数正确反映名称空间。在上面的示例中,对于超链接到用户详细信息视图的序列化器字段,您需要包括一个参数,例如view\u name='app\u name:user detail'

自动视图名称生成使用类似于%(模型名称)-详细信息的模式。除非您的模型名称实际上发生冲突,否则在使用超链接序列化程序时,最好不要给Django REST框架视图命名

试试这个

 reverse('KnownLocationView:mission-detail, kwargs={'Name': obj.gdt})

reverse('mission-detail, kwargs={'Name': obj.gdt})

相关问题 更多 >

    热门问题