[英]JSON/Django http request
I've written some django code that allows me to request some data and returns it into a JSON format. 我已经写了一些django代码,可以让我请求一些数据并将其返回为JSON格式。
I request: http http://127.0.0.0.8000/api/club
我要求:
http http://127.0.0.0.8000/api/club
[
{
"name": "Liverpool FC",
"abv": "LFC",
"stadium": "Anfield",
"manager": "Jurgen Klopp",
"trophies": "50"
},
{
"name": "Manchester City",
"abv": "MC",
"stadium": "Etihad",
"manager": "Manuel Pellegrini",
"trophies": "14"
},
{
"name": "Manchester United",
"abv": "MU",
"stadium": "Old Trafford",
"manager": "Louis Van Gaal",
"trophies": "46"
}
]
Is it possible to request only "Liverpool FC"'s data such that the request only returns. 是否可以仅请求“ Liverpool FC”的数据,以便仅返回请求。 I can do this by
http http://127.0.0.0.8000/api/club/1/
but I'd rather be able to type in a team name like http http://127.0.0.0.8000/api/club/liverpool/
我可以通过
http http://127.0.0.0.8000/api/club/1/
来做到这一点,但我希望能够输入像http http://127.0.0.0.8000/api/club/liverpool/
这样的团队名称http http://127.0.0.0.8000/api/club/liverpool/
{
"name": "Liverpool FC",
"abv": "LFC",
"stadium": "Anfield",
"manager": "Jurgen Klopp",
"trophies": "50"
}
Edit: Added two py files 编辑:添加了两个py文件
views.py views.py
# Create your views here.
class FishViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Fish.objects.all()
serializer_class = FishSerializer
# Create your views here.
class TeamViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Team.objects.all()
serializer_class = TeamSerializer
# Create your views here.
class ClubViewSet(viewsets.ModelViewSet):
# this fetches all the rows of data in the Fish table
queryset = Club.objects.all()
serializer_class = ClubSerializer
def getClub(request, club_name):
queryset = Club.objects.get(name=club_name)
models.py models.py
class Fish(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField('auto_now_add=True')
active = models.BooleanField()
class Team(models.Model):
starting = models.CharField(max_length=255)
captain = models.CharField(max_length=255)
value = models.CharField(max_length=255)
fixtures = models.CharField(max_length=255)
position = models.CharField(max_length=255)
class Club(models.Model):
name = models.CharField(max_length=255)
abv = models.CharField(max_length=255)
stadium = models.CharField(max_length=255)
manager = models.CharField(max_length=255)
trophies = models.CharField(max_length=255)
urls.py urls.py
router = routers.DefaultRouter()
#makes sure that the API endpoints work
router.register(r'api/fishes', views.FishViewSet)
router.register(r'api/teams', views.TeamViewSet)
router.register(r'api/club', views.ClubViewSet)
admin.autodiscover()
urlpatterns = router.urls
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
url(r'^/api/club/(?P<club_name>[a-zA-Z]+)/$', ('getClub'))
I know a simple way by using regex 我知道使用正则表达式的简单方法
in the url.py add this regex 在url.py中添加此正则表达式
url(r'^/api/club/(?P<club_name>[a-zA-Z]+)/$', ....)
then in the views add a method will the club_name variable like this 然后在视图中添加一个方法,将club_name变量像这样
from django.http import HttpResponse
def getclub(request, club_name):
teaminfo = ModelName.objects.get(name=club_name) # something like that
return HttpResponse(teaminfo)
the club_name variable will get the the string from the regex, you can use it in anyway you want. club_name变量将从正则表达式获取字符串,您可以随时使用它。
Here's a simple good reference for this 这是一个简单的很好的参考
https://docs.djangoproject.com/en/1.9/intro/tutorial03/
https://docs.djangoproject.com/zh-CN/1.9/intro/tutorial03/
The issue is that you're using queryset = Team.objects.all()
in your view. 问题是您在视图中使用
queryset = Team.objects.all()
。 By nature, this implies that you'll get all of the objects. 从本质上讲,这意味着您将获得所有对象。
I have a similar program, and I use urls.py
like this- 我有一个类似的程序,我像这样使用
urls.py
...
url(r'^teams/(?P<incoming_team>[^/]+)/$', ('team_stats')),
url(r'^teams/', ('team_stats_all')),
in views it looks something like: 在视图中看起来像:
def team_stats(request, incoming_team):
queryset = Team.objects.get(name=incoming_team)
Obviously your existing view would be used for all. 显然,您现有的视图将用于所有视图。 I'd also note you'll need some Try/Except handling, in case you have duplicate teams, or the team you request doesn't exist.
我还要注意,如果您有重复的团队,或者您请求的团队不存在,则需要进行“尝试/除外”处理。
(In practice I don't actually split them out like this into separate views, but it gives you an idea of the logic flow) (实际上,我实际上并没有像这样将它们分成单独的视图,但是它使您对逻辑流程有所了解)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.