我的url参数未在django中到达我的视图

时间:2018-03-19 10:55:12

标签: django django-views

url参数未到达我的视图。

我的网址定义如下,其中category_code是参数:

# posting image for a post
url(r'^post/photo/(?P<category_code>[0-9]+)/$', views.post_photo, name='post_photo_url'),

我的观点如下,我希望我的url参数category_code:

@api_view(['POST'])
def post_photo(request, category_code):
    """
    Post all the PHOTO URL for that POST
    """
    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = PhotoURLSerializer(data=data)
        if serializer.is_valid():
            photoUrl = serializer.save()
            set_thumbnail(photoUrl.post_id, category_code, photoUrl.url)
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

set_thumbnail函数如下:

def set_thumbnail(post_id, category_code, thumbnail_url):
    if category_code == 0: # mobile category
        pass

    elif category_code == 1: # Electronic category
        pass
    elif category_code == 2: # Car category
        autoPost = AutoPost.objects.get(pk=post_id)
        autoPost.set_thumbnail_url(thumbnail_url)

    elif category_code == 3: # Furniture category
        pass

    elif category_code == 4: # Fashion category
        pass
    elif category_code == 5: # Real Estate category
        housePost = RealEstate.objects.get(pk=post_id)
        housePost.set_thumbnail_url(thumbnail_url)
        housePost.save()
        pass
    elif category_code == 6: # Jobs and services category
        pass
    elif category_code == 7: # Show Category
        pass

但是当视图执行时, 函数set_thumbnail从类别代码变为null,即使从服务器日志中我可以看到参数已经通过,它是第一行,参数值是2

enter image description here

URL正在使用rest api从Android客户端接收数据。

/* PHOTO FOR POSTS */
    @POST("/post/photo/{category_code}/")
    Call<PhotoURL> createPhotoURL(@Body PhotoURL photoURL, @Path("category_code") int categoryCode);

1 个答案:

答案 0 :(得分:1)

category_code是一个字符串,因此不会等同于set_thumbnail方法中的任何一个整数。将其转换为int或使用字符串进行比较。