最近在學(xué)習(xí)Django,跟著做一個Django論壇從搭建到部署,教程鏈接也貼上:【第一部分-django論壇從搭建到部署】一個完整Django入門指南-蒼云橫渡页畦,做到第三部分時候遇到一個問題,困擾了我很久。經(jīng)查閱后仍是無果棚赔,官方文檔也看了依然沒有解決問題,后來突然發(fā)現(xiàn)了問題徘郭,再看看報錯的情況靠益,確實就是自己寫的時候?qū)戝e了。先把錯誤情況貼上:
Internal Server Error: /boards/1/ Traceback (most recent call last): File "D:\IDE\Python\Anaconda\lib\site-packages\django\core\handlers\ex ception.py", line 34, in inner response = get_response(request) File "D:\IDE\Python\Anaconda\lib\site-packages\django\core\handlers\ba se.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "D:\IDE\Python\Anaconda\lib\site-packages\django\core\handlers\ba se.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwar gs) File "D:\IDE\pycharm\PyCharm Community Edition 2018.1.2\DjangoProjects \myproject\boards\views.py", line 33, in board_topics return render(request, 'topics.html', {'board': board}) File "D:\IDE\Python\Anaconda\lib\site-packages\django\shortcuts.py", l ine 36, in render content = loader.render_to_string(template_name, context, request, u sing=using) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\loader. py", line 62, in render_to_string return template.render(context, request) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\backend s\django.py", line 61, in render return self.template.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 171, in render return self._render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 163, in _render return self.nodelist.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 937, in render bit = node.render_annotated(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 904, in render_annotated return self.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\loader_ tags.py", line 150, in render return compiled_parent._render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 163, in _render return self.nodelist.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 937, in render bit = node.render_annotated(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 904, in render_annotated return self.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\loader_ tags.py", line 62, in render result = block.nodelist.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 937, in render bit = node.render_annotated(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 904, in render_annotated return self.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\loader_ tags.py", line 62, in render result = block.nodelist.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 937, in render bit = node.render_annotated(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\base.py ", line 904, in render_annotated return self.render(context) File "D:\IDE\Python\Anaconda\lib\site-packages\django\template\default tags.py", line 442, in render url = reverse(view_name, args=args, kwargs=kwargs, current_app=curre nt_app) File "D:\IDE\Python\Anaconda\lib\site-packages\django\urls\base.py", l ine 90, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "D:\IDE\Python\Anaconda\lib\site-packages\django\urls\resolvers.p y", line 622, in _reverse_with_prefix raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'new_topic' not found . 'new_topic' is not a valid view function or pattern name. [24/Nov/2018 22:54:00] "GET /boards/1/ HTTP/1.1" 500 169427
看起來有點亂残揉,其實關(guān)鍵信息就是最后一句話:
Reverse for 'new_topic' not found . 'new_topic' is not a valid view function or pattern name.?
意思就是new_topic在反析的時候沒有被找到胧后,'new_topic' 不是一個有效的視圖方法或者模式名。
瀏覽器報的錯也貼上:
urls.py:
view.py:
我注意到其實是urls.py文件中命名空間寫錯了:
實際上我在view.py文件中定義的方法是new_topic抱环,HTML模板命名也是new_topic.html壳快,所以多加了一個s導(dǎo)致了錯誤。以后還是需要更加細心镇草。