【问题标题】:ImportError: cannot import name 'views' from '__main__' (C:\Users\Kalyan Mohanty\Documents\GitHub\Django\rek\calc\urls.py)ImportError: 无法从 '__main__' 导入名称 'views' (C:\Users\Kalyan Mohanty\Documents\GitHub\Django\rek\calc\urls.py)
【发布时间】:2020-08-24 22:50:03
【问题描述】:

我正在学习 Django,但在构建我的第一个 Django 的过程中,我遇到了这个问题。

ImportError: cannot import name 'views' from '__main__' (C:\Users\Kalyan Mohanty\Documents\GitHub\Django\rek\calc\urls.py)

在我的应用“计算”​​文件夹下

urls.py

from django.urls import path from . import views urlspatterns = [ path('', views.home, name = 'home') ]

views.py

from django.shortcuts import render from django.http import HttpResponse from django.contrib import admin

def home(request): return HttpResponse('Hello world')

rek 文件夹 urls.py

from django.contrib import admin from django.urls import path, include

urlpatterns = [ path('', include('calc.urls')), path('admin/', admin.site.urls), ]

结构

-calc (folder)

 `-__pycache__ (folder)`

 `-migrations (folder)`

 `-__init__`

 `-admin.py`

 `-apps.py`

 `-models.py`

 `-test.py`

 `-urls.py`

 `-views.py`

-rek (folder)

 `-__pycache__`

 `-__init__`

 `-settings.py`

 `-urls.py`

 `-wsgi.py`

db.sqlite3

manage.py

【问题讨论】:

    标签: python django python-3.x


    【解决方案1】:

    你可以只使用导入视图。这对我有用

    【讨论】:

    • 我以前试过。但是在运行服务器时出现以下错误。 ModuleNotFoundError: No module named 'views'
    • 如果我使用的是 from . import views 。在运行服务器时出现django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'calc.urls' from 'C:\\Users\\Kalyan Mohanty\\Documents\\GitHub\\Django\\rek\\calc\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import. 错误
    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2023-03-15
    • 2019-09-18
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多