为什么 AngularJS 将 DRF API 返回的整数“3”读取为“%203”?
Why does AngularJS read integer `3` returned by DRF API as `%203`?
这是我的 DRF 视图,它在 /posts/{id}
:
被调用
class PostViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.DestroyModelMixin, GenericViewSet):
serializer_class = PostSerializer
permission_classes = (IsAuthenticated, IsActualOwnerDelete,)
def get_queryset(self):
# new posts from location.
return Post.objects.filter(location=self.request.user.userextended.location).order_by('-createdAt')
def perform_create(self, serializer):
serializer.save(actualOwner=self.request.user)
@list_route(permission_classes=[IsAuthenticated])
def mymostrecent(self, request):
me = request.user
try:
# this gets the most recent post's ID
post = me.post_mine_set.latest('id')
did = post.id
except Post.DoesNotExist:
did = 0
return Response(did)
现在用 Angular,当我对 URL /posts/mymostrecent
:
return $http.get("/api/posts/mymostrecent")
.then(function(response) {
console.log(response);
$location.url('/posts/ ' + response.data);
})
记录的内容是:
Object {data: 3, status: 200, config: Object, statusText: "OK"}
但是 URL 变成了这样:
/posts/%203
它仍然有效并显示正确的 html 页面,但是如何去掉 URL 中的 %20
?
这是我的models.py
(可能不需要):
class Post(models.Model):
owner = models.ForeignKey(User)
usersVoted = models.ManyToManyField(User, blank=True, related_name="%(class)s_voted_set")
post = models.CharField(max_length=400)
createdAt = models.DateTimeField(auto_now_add=True, blank=True)
actualOwner = models.ForeignKey(User, related_name="%(class)s_mine_set")
这部分代码有问题,您在这一行添加了一个额外的 space $location.url('/posts/ ' + response.data);
return $http.get("/api/posts/mymostrecent")
.then(function(response) {
console.log(response);
$location.url('/posts/' + response.data); #Removed the space after '/posts/ ' ie. change '/posts/ ' to '/posts/'
})
这是我的 DRF 视图,它在 /posts/{id}
:
class PostViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.DestroyModelMixin, GenericViewSet):
serializer_class = PostSerializer
permission_classes = (IsAuthenticated, IsActualOwnerDelete,)
def get_queryset(self):
# new posts from location.
return Post.objects.filter(location=self.request.user.userextended.location).order_by('-createdAt')
def perform_create(self, serializer):
serializer.save(actualOwner=self.request.user)
@list_route(permission_classes=[IsAuthenticated])
def mymostrecent(self, request):
me = request.user
try:
# this gets the most recent post's ID
post = me.post_mine_set.latest('id')
did = post.id
except Post.DoesNotExist:
did = 0
return Response(did)
现在用 Angular,当我对 URL /posts/mymostrecent
:
return $http.get("/api/posts/mymostrecent")
.then(function(response) {
console.log(response);
$location.url('/posts/ ' + response.data);
})
记录的内容是:
Object {data: 3, status: 200, config: Object, statusText: "OK"}
但是 URL 变成了这样:
/posts/%203
它仍然有效并显示正确的 html 页面,但是如何去掉 URL 中的 %20
?
这是我的models.py
(可能不需要):
class Post(models.Model):
owner = models.ForeignKey(User)
usersVoted = models.ManyToManyField(User, blank=True, related_name="%(class)s_voted_set")
post = models.CharField(max_length=400)
createdAt = models.DateTimeField(auto_now_add=True, blank=True)
actualOwner = models.ForeignKey(User, related_name="%(class)s_mine_set")
这部分代码有问题,您在这一行添加了一个额外的 space $location.url('/posts/ ' + response.data);
return $http.get("/api/posts/mymostrecent")
.then(function(response) {
console.log(response);
$location.url('/posts/' + response.data); #Removed the space after '/posts/ ' ie. change '/posts/ ' to '/posts/'
})