Python编程从入门到实践-18章映射url:AttributeError: module ‘learning_logs.views‘ has no attribute ‘index‘

Python编程从入门到实践-18章映射url:AttributeError: module ‘learning_logs.views’ has no attribute 'index’和django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include()报错

问题描述:

Python编程从入门到实践-18章映射url:AttributeError: module ‘learning_logs.views’ has no attribute 'index’和django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include()报错

原因分析:

主要是Python编程从入门到实践书中使用Django1.1实验,目前安装Djang2.0,两个版本存在语法差异

解决方案:

网上看了很多教程最终解决方法:
learning_log\urls.py
from django.urls import path, include
from django.contrib import admin

urlpatterns = [
path(‘admin/’, admin.site.urls),
path(’’, include(‘learning_logs.urls’,namespace=‘learning_logs’)),
]

修改完成后:在earning_logs文件夹中新建urls.py文件:
“”“定义learning_logs的URL模式”""
from django.urls import path

from . import views

app_name = ‘learning_logs’

urlpatterns = [
# 主页
path(r’’, views.index, name=‘index’),
]

此时运行服务器:python manage.py runserver
报错:AttributeError: module ‘learning_logs.views’ has no attribute ‘index’
是因为learning_logs.views’ 没用定义 'index’函数,只是不用管往下一步章节做,就会创建 ‘index’函数,
或者打开learning_logs中的’view.py’
修改:
from django.shortcuts import render

from .models import Topic

def index(request):
“”“The home page for Learning Log.”""
return render(request, ‘learning_logs/index.html’)

而后成功运行服务器

你可能感兴趣的:(python,django)