找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 169|回复: 2

[求助] python+django使用继承模板,无法显示web页面

1

主题

1

帖子

1

积分

贫民

积分
1
zw1068 发表于 2019-4-8 15:18:44 | 显示全部楼层 |阅读模式
做“学习笔记”项目,18.4章,用模版继承实现多网页,父模板base.html,子模版index.html,topics.html,
始终无法正常显示网页,刚开始主页还能显示,用了继承之后,主页也显示不了了。


下面是症状
1.输入localhost:8000/topics/
网页显示:A server error occurred. Please contact the administrator.
报错
  1. [font=Times New Roman][size=4]Traceback (most recent call last):
  2.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\w
  3. sgiref\handlers.py", line 137, in run
  4.     self.result = application(self.environ, self.start_resp**e)
  5.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  6. ite-packages\django\contrib\staticfiles\handlers.py", line 65, in __call__
  7.     return self.application(environ, start_resp**e)
  8.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  9. ite-packages\django\core\handlers\wsgi.py", line 141, in __call__
  10.     resp**e = self.get_resp**e(request)
  11.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  12. ite-packages\django\core\handlers\base.py", line 75, in get_resp**e
  13.     resp**e = self._middleware_chain(request)
  14.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  15. ite-packages\django\core\handlers\exception.py", line 36, in inner
  16.     resp**e = resp**e_for_exception(request, exc)
  17.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  18. ite-packages\django\core\handlers\exception.py", line 90, in resp**e_for_exce
  19. ption
  20.     resp**e = handle_uncaught_exception(request, get_resolver(get_urlconf()),
  21. sys.exc_info())
  22.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  23. ite-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_
  24. exception
  25.     return debug.technical_500_resp**e(request, *exc_info)
  26.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  27. ite-packages\django\views\debug.py", line 94, in technical_500_resp**e
  28.     html = reporter.get_traceback_html()
  29.   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\s
  30. ite-packages\django\views\debug.py", line 332, in get_traceback_html
  31.     t = DEBUG_ENGINE.from_string(fh.read())
  32. UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illeg
  33. al multibyte sequence
  34. [/size][/font]
复制代码
2.输入localhost:8000/
网页显示:A server error occurred. Please contact the administrator.
报错跟上边一样3.输入localhost:8000/admin/
网页显示正常
下面是我的代码
这是learning_log/setting
  1. <font size="4" face="Times New Roman">INSTALLED_APPS = [
  2.     'django.contrib.admin',
  3.     'django.contrib.auth',
  4.     'django.contrib.sites',
  5.     'django.contrib.contenttypes',
  6.     'django.contrib.sessi**',
  7.     'django.contrib.messages',
  8.     'django.contrib.staticfiles',
  9.     #my app
  10.     'learning_logs',</font>
复制代码


这是learning_log/urls
  1. <font size="4" face="Times New Roman">from django.contrib import admin
  2. from django.urls import include,path
  3. urlpatterns = [
  4.     path('admin/', admin.site.urls),
  5.     path('',include('learning_logs.urls',namespace='learning_logs')),
  6. ]
  7. </font>
复制代码

这是learning_logs/urls
  1. <font size="4" face="Times New Roman">from django.urls import path,re_path
  2. from . import views
  3. app_name = 'learning_logs'
  4. urlpatterns = [
  5.     path('',views.index,name='index'),
  6.     path('topics/',views.topics,name='topics'),</font>
复制代码
这是learning_logs/views
  1. <font size="4" face="Times New Roman">from django.shortcuts import render
  2. from . models import Topic
  3. # Create your views here.
  4. def index(request):
  5.     '''学习笔记的主页'''
  6.     return render(request,'learning_logs/index.html')
  7. def topics(request):
  8.     '''显示所有的主题'''
  9.     topics = Topic.objects.order_by('date_added')
  10.     context = {'topics':topics}
  11.     return render(request,'learning_logs/topics.html',context)</font>
复制代码


这是base.html
  1. <font size="4" face="Times New Roman"><p>
  2.     <a href="{% url 'learning_logs:index' %}">Learning Log</a> -
  3.     <a href="{% url 'learning_logs:topcis' %}">Topics</a>
  4. </p>
  5. {% block content %}{% endblock content %}
  6. </font>
复制代码

这是index.html
  1. <font size="4" face="Times New Roman">{% extends "learning_logs/base.html" %}
  2. {% block content %}
  3.   <p>Learning Log helps you keep track of your learning,for any topic you're learning about.</p>
  4. {% endblock content %}</font>
复制代码
这是topics.html
  1. <font size="4" face="Times New Roman">{% extends "learning_logs/base.html" %}-->
  2. {% block content %}
  3. <p>Topics</p>
  4. <ul>
  5.     {% for topic in topics %}
  6.       <li> {{ topic }} </li>
  7.     {% empty %}
  8.       <li>No topics have been added yet.</li>
  9.     {% endfor %}
  10. </ul>
  11. {% endblock content %}
  12. </font>
复制代码


回复

使用道具 举报

0

主题

421

帖子

421

积分

骑士

积分
421
sheeboard 发表于 2019-4-8 15:56:45 | 显示全部楼层
编码错误,用utf8,windows默认是gbk
回复 支持 反对

使用道具 举报

0

主题

3

帖子

3

积分

贫民

积分
3
eruy 发表于 2019-4-9 17:39:47 | 显示全部楼层
  1. UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illeg
  2. al multibyte sequence
复制代码


看错误提示呀。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表