配置urls.py, url('^login/$', views.login, name='login'), 调试启动服务器报错。
来源:6-2 用户登录-1
慕粉105744713
2018-08-08
"C:\Program Files\JetBrains\PyCharm 2018.1.2\bin\runnerw.exe" C:\Users\lizhi\Envs\mxonline\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 53474 --file D:/MxOnline/manage.py runserver 8000
pydev debugger: process 4940 is connecting
Connected to pydev debugger (build 181.4668.75)
pydev debugger: process 10252 is connecting
Performing system checks...
Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x000002491F6C94E0>
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 589, in __call__
return self.original_func(*self.args, **self.kwargs)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\lizhi\Envs\mxonline\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\lizhi\Envs\mxonline\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\MxOnline\MxOnline\urls.py", line 27, in <module>
url('^login/$', views.login, name='login'),
AttributeError: module 'users.views' has no attribute 'login'
-----------------------------------------------------------------
相关代码如下:
views.py
from django.shortcuts import render
# Create your views here.
def login(request):
if request.method == 'POST':
pass
elif request.method == 'GET':
return render(request, 'login.html', {})
------------------------------------------------
urls.py :
from django.conf.urls import url
from django.contrib import admin
from django.views.generic import TemplateView
import xadmin
from users import views
urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
url('^$', TemplateView.as_view(template_name='index.html'), name='index'),
url('^login/$', views.login, name='login'),
]
2回答
-
慕粉105744713
提问者
2018-08-08
Bobby 老师,
从错误信息来看: url('^login/$', views.login, name='login'),
AttributeError: module 'users.views' has no attribute 'login'
的确是说,users.views 中没有 login。 以下是
views.py 内容: 其中明确定义了 login() 函数,以下加粗部分。
from django.shortcuts import render
# Create your views here.
def login(request):
if request.method == 'POST':
pass
elif request.method == 'GET':
return render(request, 'login.html', {})012018-08-10 -
bobby
2018-08-08
users.views 中没有login这个函数 你检查一下你的view
022018-08-08
相似问题