Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django rest框架中返回Respose对象时出现NoReverseMatch错误_Python_Django_Django Rest Framework - Fatal编程技术网

Python 在django rest框架中返回Respose对象时出现NoReverseMatch错误

Python 在django rest框架中返回Respose对象时出现NoReverseMatch错误,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在尝试构建Instagram克隆 单击follow按钮调用ajax My view def post保存“follows”并返回其数据为true/false值的响应对象,以确定用户之前是否被跟踪 错误消息 ‘django.urls.exceptions.NoReverseMatch: Reverse for 'profile' with arguments '('',)' not found. 2 pattern(s) tried’ views.py class ProfileListV

我正在尝试构建Instagram克隆

单击follow按钮调用ajax

My view def post保存“follows”并返回其数据为true/false值的响应对象,以确定用户之前是否被跟踪

错误消息

‘django.urls.exceptions.NoReverseMatch: Reverse for 'profile' 
with arguments '('',)' not found. 2 pattern(s) tried’
views.py

class ProfileListView(mixins.UpdateModelMixin,generics.GenericAPIView):
“”“用户帖子列表”“”
渲染器\类=[TemplateHTMLRenderer]
模板名称='insta/profile\u list.html'
serializer\u class=InstaSerializer
权限\类=[permissions.AllowAny]
def get(自我、请求、*args、**kwargs):
target=kwargs.get('username')
尝试:
target\u user=user.objects.get(用户名=target)
响应=Insta.objects.filter(所有者\用户名=目标)
返回响应({'posts':响应,'target\u user':target\u user})
除USER.DoesNotExist外:
返回HttpResponseRedirect(反向('insta:dashboard'),状态=HTTP\u 404\u未找到)
def post(自我、请求、*args、**kwargs):
跟随的\u user=get\u object\u或\u 404(用户,用户名=kwargs.get('username'))
如果request.user.u经过身份验证:
follower\u user=request.user
如果跟随者用户==跟随者用户:
raise PermissionError('无法跟踪您自己')
其他:
如果follower\u user在followerd\u user.followers.all()中:
跟随者用户。跟随者。移除(跟随者用户)
返回响应({
“follow_exist”:False
})
其他:
follower\u user.follows.add(followerd\u user)
返回响应({
“follow_exist”:True
})
其他:
返回重定向('insta/login')
url.py

path('/',insta_profile,name='profile'),
阿贾克斯

你能告诉我为什么会这样吗


提前谢谢。

请在例外情况下添加此项

 return HttpResponseRedirect(reverse('insta:dashboard', args=(username,), status=HTTP_404_NOT_FOUND)
而不是

    return HttpResponseRedirect(reverse('insta:dashboard'), status=HTTP_404_NOT_FOUND)

请参阅在例外情况下添加此项

 return HttpResponseRedirect(reverse('insta:dashboard', args=(username,), status=HTTP_404_NOT_FOUND)
而不是

    return HttpResponseRedirect(reverse('insta:dashboard'), status=HTTP_404_NOT_FOUND)

请参考

我认为target_user.username在ajax url中变为空白,可以尝试以下方法:
url:“{%url”profile“target_user.username%”,
也可以
insta_profile
它在url中指向何处?请显示呈现包含ajax的模板的视图。
带参数(“”,)“
表示
target\u user
未设置,或者
target\u user.username
未设置,或者错误来自未显示的另一个
{%url%}
标记。这是什么时候发生的?当你真正点击“跟随”按钮时?或者当您呈现该按钮应显示的模板时?我认为target_user.username在ajax url中变为空白,请尝试以下操作:
url:“{%url”profile“target_user.username%}”,
还有
insta_profile
它在您的url中指向哪里?请显示您呈现包含ajax的模板的视图。
带有参数“(”,)”
意味着
目标用户
未设置,或者
目标用户。用户名
未设置,或者错误来自不同的
{%url%}
您尚未显示的标签。这是什么时候发生的?当你真正点击“跟随”按钮时?或者当您呈现该按钮应显示的模板时?谢谢您的回答,但它不会引发DoesNotExist谢谢您的回答,但它不会引发DoesNotExist