繁体   English   中英

如何使用 django-rest-framework 获取 url 的参数

[英]How can I get a parameter of an url with django-rest-framework

我正在建造一个具有不同车站位置的 map。 这些站属于不同的领域。 我必须显示所有站点,并且我有一个字段的所有站点。

在某些时候我的 api 被称为

"""Markers API URL Configuration."""
# Maps with Django (2)
# https://www.paulox.net/2021/07/19/maps-with-django-part-2-geodjango-postgis-and-leaflet/
from rest_framework import routers
from map.viewsets import MarkerViewSet

router = routers.DefaultRouter()
router.register(r"map", MarkerViewSet)
urlpatterns = router.urls

然后调用 MarkerViewSet (viewsets.py)

class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
    """Marker view set."""
    #print(self.kwargs.get('idfield'))
    bbox_filter_field = "location"
    filter_backends = (filters.InBBoxFilter,)
    queryset = Stations.objects.filter(station_active=1, map=1)
    serializer_class = StationsSerializer

    def get_queryset(self, **kwargs):
        #field_id = self.kwargs['idfield']
        #print("pp:", self.kwargs)
        return Stations.objects.filter(fields_id_field=1)

上面的代码有效,但它只显示字段 1 的标记

如果我将其更改为 2

return Stations.objects.filter(fields_id_field=2)

它显示了 id 为 2 的字段的所有站点

我的 url 如下http://127.0.0.1:8080/map/打印所有领域的所有站点。 如果我想拥有字段 1 的站点,我的 url 将是

http://127.0.0.1:8080/map/1/field

对于字段 2 和 4

http://127.0.0.1:8080/map/2/field
http://127.0.0.1:8080/map/4/field

我的问题,我不能,我不知道我的 url 得到 id 1,2,4 或其他形式

这不起作用

#field_id = self.kwargs['idfield']
#print("pp:", self.kwargs)

这是我的map/urls.py和我的项目urls.py

我应该尝试从viewets.py 中获取该字段的ID,还是建议我如何归档我的目标?

编辑: 这是我的 views.py 在这里我可以得到 idfield 有没有办法将它传递给我的 viewsets.py

来自 GET 请求的kwargs可以如下获取

request.GET.get("idfield")

我相信你的提议只适用于views.py,因为它开始于

def field(request, idfield):
    request.GET.get("idfield")

不是吗?

我正在使用viewsets.py,从

class MarkerViewSet(viewsets.ReadOnlyModelViewSet):
    """Marker view set."""
    bbox_filter_field = "location"
    filter_backends = (filters.InBBoxFilter,)
    queryset = Stations.objects.filter(station_active=1, map=1)
    serializer_class = StationsSerializer
    """
    def get_tags(self):
        return Stations.objects.filter(fields_id_field=1)
    """
    def get_queryset(self):
        #field_id = self.kwargs['idfield']
        #print("ppp:", self.kwargs['idfield'])
        return Stations.objects.filter(fields_id_field=1)

我确实知道您的提议可以添加到哪里。

但是,我认为我已经接近解决方案,但仍然出现错误。

我希望你能帮帮我

首先,在我的 map.js 文件中。 我添加了

async function load_markers() {
    const markers_url = `/api/map/1/`; // LOOK AT THIS LINE
    console.log("markers_url: ",markers_url);
    const response = await fetch(markers_url);
    //console.log("response: ",response);
    const geojson = await response.json();
    console.log("geojson: ",geojson);
    return geojson;
}

在我的 urls.py 文件中,我改为

path("api/<int:idf>/", include("map.api")),

这将重定向到 api.py 文件。 我认为,直到这一点,一切都是正确的,不是吗?

在我的 api.py 文件中,我尝试了一些从django-rest-framework - 路由器页面获得灵感的东西

router = routers.DefaultRouter()
#router.register(r"map", MarkerViewSet) #INITIAL
#router.register(r"^map/{idf}/$", MarkerViewSet) #I tried this
#router.register(r"^map/(?P<idf>[0-9]+)/$", MarkerViewSet) #this generate an error because of idf 
router.register(r"^map/(?P<id_f>[0-9]+)/$", MarkerViewSet)
urlpatterns = router.urls

Firefox 上有另一条错误消息:在此处输入图像描述

我相信,我应该找到正确的格式,但其他地方应该是错误的

非常感谢您的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM