做“学习笔记”项目,18.4章,用模版继承实现多网页,父模板base.html,子模版index.html,topics.html,
始终无法正常显示网页,刚开始主页还能显示,用了继承之后,主页也显示不了了。
下面是症状
1.输入localhost:8000/topics/
网页显示:A server error occurred. Please contact the administrator. 报错
- [font=Times New Roman][size=4]Traceback (most recent call last):
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\w
- sgiref\handlers.py", line 137, in run
- self.result = application(self.environ, self.start_resp**e)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\contrib\staticfiles\handlers.py", line 65, in __call__
- return self.application(environ, start_resp**e)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\core\handlers\wsgi.py", line 141, in __call__
- resp**e = self.get_resp**e(request)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\core\handlers\base.py", line 75, in get_resp**e
- resp**e = self._middleware_chain(request)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\core\handlers\exception.py", line 36, in inner
- resp**e = resp**e_for_exception(request, exc)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\core\handlers\exception.py", line 90, in resp**e_for_exce
- ption
- resp**e = handle_uncaught_exception(request, get_resolver(get_urlconf()),
- sys.exc_info())
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_
- exception
- return debug.technical_500_resp**e(request, *exc_info)
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\views\debug.py", line 94, in technical_500_resp**e
- html = reporter.get_traceback_html()
- File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
- ite-packages\django\views\debug.py", line 332, in get_traceback_html
- t = DEBUG_ENGINE.from_string(fh.read())
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illeg
- al multibyte sequence
- [/size][/font]
复制代码2.输入localhost:8000/ 网页显示:A server error occurred. Please contact the administrator. 报错跟上边一样3.输入localhost:8000/admin/
网页显示正常
下面是我的代码
这是learning_log/setting
- <font size="4" face="Times New Roman">INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.sites',
- 'django.contrib.contenttypes',
- 'django.contrib.sessi**',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- #my app
- 'learning_logs',</font>
复制代码
这是learning_log/urls
- <font size="4" face="Times New Roman">from django.contrib import admin
- from django.urls import include,path
- urlpatterns = [
- path('admin/', admin.site.urls),
- path('',include('learning_logs.urls',namespace='learning_logs')),
- ]
- </font>
复制代码
这是learning_logs/urls
- <font size="4" face="Times New Roman">from django.urls import path,re_path
- from . import views
- app_name = 'learning_logs'
- urlpatterns = [
- path('',views.index,name='index'),
- path('topics/',views.topics,name='topics'),</font>
复制代码 这是learning_logs/views
- <font size="4" face="Times New Roman">from django.shortcuts import render
- from . models import Topic
- # Create your views here.
- def index(request):
- '''学习笔记的主页'''
- return render(request,'learning_logs/index.html')
- def topics(request):
- '''显示所有的主题'''
- topics = Topic.objects.order_by('date_added')
- context = {'topics':topics}
- return render(request,'learning_logs/topics.html',context)</font>
复制代码
这是base.html
- <font size="4" face="Times New Roman"><p>
- <a href="{% url 'learning_logs:index' %}">Learning Log</a> -
- <a href="{% url 'learning_logs:topcis' %}">Topics</a>
- </p>
- {% block content %}{% endblock content %}
- </font>
复制代码
这是index.html
- <font size="4" face="Times New Roman">{% extends "learning_logs/base.html" %}
- {% block content %}
- <p>Learning Log helps you keep track of your learning,for any topic you're learning about.</p>
- {% endblock content %}</font>
复制代码 这是topics.html
- <font size="4" face="Times New Roman">{% extends "learning_logs/base.html" %}-->
- {% block content %}
- <p>Topics</p>
- <ul>
- {% for topic in topics %}
- <li> {{ topic }} </li>
- {% empty %}
- <li>No topics have been added yet.</li>
- {% endfor %}
- </ul>
- {% endblock content %}
- </font>
复制代码
|