当我执行 python manage.py runserver 时,CMD 抛出 AttributeError:

Posted

技术标签:

【中文标题】当我执行 python manage.py runserver 时,CMD 抛出 AttributeError:【英文标题】:when I execute the python manage.py runserver, CMD throws AttributeError: 【发布时间】:2020-10-11 15:56:18 【问题描述】:

我是 Django 新手!所以我使用 python/Django 构建了一个简单的博客应用程序,mysql 作为后端使用 XAMPP 控制面板。它在 http://localhost:8000 上按预期运行。但是,在项目结束时,当我添加按钮功能并使用 CMD 重新启动(python manage.py runserver)服务器时。 CMD 抛出:AttributeError: module 'posts.views' has no attribute 'details'。

您的帮助将不胜感激!

-附件是GitHub上的项目。

urls.py 文件:

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^details/(?P<id>\d+)/$', views.details, name='details')
]

views.py 文件:

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

from .models import Posts

# Create your views here.
def index(request):
# return HttpResponse('HELLO FROM POSTS')

posts = Posts.objects.all()[:10]

context = 
   'title': 'Latest Posts',
   'posts': posts


return render(request, 'posts/index.html', context)

def details(request, id):
   post = Posts.object.get(id=id)

   context = 
       'post': post
   

   return render(request, 'posts/details.html', context)

details.py 文件:

% extends 'posts/layout.html' %

% block content%
<h3 class="center-align red lighten-3">post.title</h3>
<div class = "card">
<div class="card-content">
    post.body
</div>
<div class="card-action">
    post.created_at
</div>
</div>
<a href="/posts" class="btn">Go Back</a>
% endblock %

错误堆栈跟踪:

(py1) C:\Users\Ajmal .M\Google Drive\GitHub\Projects\djangoproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
File "c:\users\ajmal .m\appdata\local\programs\python\python38\lib\threading.py", line 932, in 
_bootstrap_inner
self.run()
File "c:\users\ajmal .m\appdata\local\programs\python\python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\management\commands\runserver.py", 
line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\management\base.py", line 392, in 
check
all_issues = self._run_checks(
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\management\base.py", line 382, in 
_run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\checks\registry.py", line 72, in 
run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\checks\urls.py", line 13, in 
check_url_config
return check_resolver(resolver)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\core\checks\urls.py", line 23, in 
check_resolver
return check_method()
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\urls\resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\urls\resolvers.py", line 588, in 
url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\urls\resolvers.py", line 581, in 
urlconf_module
return import_module(self.urlconf_name)
File "c:\users\ajmal .m\appdata\local\programs\python\python38\lib\importlib\__init__.py", line 127, 
in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Ajmal .M\Google Drive\GitHub\Projects\djangoproject\djangoproject\urls.py", line 5, in 
<module>
url(r'^$', include('posts.urls')),
File "C:\Users\Ajmal .M\Envs\py1\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "c:\users\ajmal .m\appdata\local\programs\python\python38\lib\importlib\__init__.py", line 127, 
in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Ajmal .M\Google Drive\GitHub\Projects\djangoproject\posts\urls.py", line 6, in 
<module>
url(r'^details/(?P<id>\d+)/$', views.details, name='details')
AttributeError: module 'posts.views' has no attribute 'details'

GitHub Link for the Project files

【问题讨论】:

urls.pyviews.py 可能不在同一个模块(目录)中? 他们是——刚刚再次确认! 您的 GitHub 链接已损坏,您的代码缩进需要修复 如果您的详细信息功能视图存在于任何特定应用程序中,则将 view.details 更正为 from your_app import views as your_app_viewsurl(r'^details/(?P&lt;id&gt;\d+)/$', your_app_views.details, name='details') 【参考方案1】:

您可能希望在 views.py 中添加一个 View 类。像这样:

from django.views import View
class Details(View):
    def get(request, id):
    post = Posts.object.get(id=id)

    context = 
        'post': post
    
    return render(request, 'posts/details.html', context)

这可能需要将您的 urls.py 更改为:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^details/(?P<id>\d+)/$', views.details.as_view(), name='details')
]

(注意.as_view()。)

【讨论】:

以上是关于当我执行 python manage.py runserver 时,CMD 抛出 AttributeError:的主要内容,如果未能解决你的问题,请参考以下文章

当我执行 python manage.py runserver 时,CMD 抛出 AttributeError:

Django的manage.py runserver 可以长期运行吗

Django的manage.py runserver 可以长期运行吗

为啥我在运行时出现整数精度错误:heroku run python manage.py migrate

python3 manage.py runserver 错误

django 和 python ./manage.py makemigrations 执行错误