我的url参数未到达django中的我的视图
url参数未到达我的视图 我的URL定义如下,其中类别代码是参数:我的url参数未到达django中的我的视图,django,django-views,Django,Django Views,url参数未到达我的视图 我的URL定义如下,其中类别代码是参数: # posting image for a post url(r'^post/photo/(?P<category_code>[0-9]+)/$', views.post_photo, name='post_photo_url'), 设置缩略图功能如下所示: def set_thumbnail(post_id, category_code, thumbnail_url): if category_code
# posting image for a post
url(r'^post/photo/(?P<category_code>[0-9]+)/$', views.post_photo, name='post_photo_url'),
设置缩略图功能如下所示:
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_缩略图从类别代码中变为null,尽管从服务器日志中我可以看到参数已经传递,它是第一行,参数值为2
URL使用RESTAPI从android客户端接收数据
/* PHOTO FOR POSTS */
@POST("/post/photo/{category_code}/")
Call<PhotoURL> createPhotoURL(@Body PhotoURL photoURL, @Path("category_code") int categoryCode);
/*帖子图片*/
@POST(“/POST/photo/{category_code}/”)
调用createPhotoURL(@Body PhotoURL PhotoURL,@Path(“category_code”)int categoryCode);
category\u code
是一个字符串,因此不等于set\u-thumbnail
方法中的任何整数。将其转换为int或使用字符串进行比较。什么是set\u缩略图
?显示代码。我刚刚编辑了帖子并加入了set\u缩略图
功能。它现在可以工作了,但是我不明白参数category\u code
如何作为字符串到达服务器,尽管它是从安卓客户端声明为整数的。Django怎么知道你的安卓客户端做了什么?它只获取URL-/post/category/2/,并使用正则表达式解析出“2”。但它仍然是一根线。哦,我明白了。当我手动将其转换为int时,它甚至更加健壮。谢谢。
/* PHOTO FOR POSTS */
@POST("/post/photo/{category_code}/")
Call<PhotoURL> createPhotoURL(@Body PhotoURL photoURL, @Path("category_code") int categoryCode);