我是靠谱客的博主 酷炫灰狼,这篇文章主要介绍python项目NoReverseMatch: Reverse for ‘topic‘ with arguments ‘(‘‘,)‘ not found解决方法,现在分享给大家,希望可以做个参考。
文章目录
- 遇到的问题
- 解决方法
- 参考
遇到的问题
今天在阅读《python编程-从入门到实践》这本书,并且课本上的项目之时,在第18章:将显示所有主题的页面中的每个主题都设置为链接的代码中,遇到了错误
复制代码
1
2
3
4
5
6File "D:user文档pythonpython_worklearning_logll_envlibsite-packagesdjangourlsbase.py", line 86, in reverse return resolver._reverse_with_prefix(view, prefix, *args, **kwargs) File "D:user文档pythonpython_worklearning_logll_envlibsite-packagesdjangourlsresolvers.py", line 694, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topics/(?P<topic_id>\d+)/$']
这个问题在于笔者修改了topics.html文件,下面是出错的版本:问题出在 链接href那里, 需要的是topic.id
,由于笔者手误,写成了tipic_id,导致反向匹配找不到。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{% extends "learning_logs/base.html" %} {% block content %} <p>Topics</p> <ul> {% for topic in topics %} <li> <a href="{% url 'learning_logs:topic' topic_id %}">{{ topic }}</a> </li> {% empty %} <li>No topics have been added yet.</li> {% endfor %} </ul> {% endblock content %}
当点击topics时,就会出现问题:
解决方法
检查自己写的代码,和书本上进行对照,一般是写错了某个地方。笔者将topic_id改为topic.id后,解决了问题。
正确的代码:
topics.html
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{% extends "learning_logs/base.html" %} {% block content %} <p>Topics</p> <ul> {% for topic in topics %} <li> <a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a> </li> {% empty %} <li>No topics have been added yet.</li> {% endfor %} </ul> {% endblock content %}
参考
[1]https://blog.csdn.net/qq_20379795/article/details/80889068
最后
以上就是酷炫灰狼最近收集整理的关于python项目NoReverseMatch: Reverse for ‘topic‘ with arguments ‘(‘‘,)‘ not found解决方法的全部内容,更多相关python项目NoReverseMatch:内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复