我的Django项目中有一些应用程序,除了User应用程序之外,它们似乎都在工作。所有应用程序都安装在设置中。每当我输入“python manage.py runserver”时,我会看到一长串代码以此结尾:
File "/Users/Name/Desktop/Project_Name/MyProject/User/urls.py", line 5,
in <module>
url(r'Home/', views.User, name='Home'),
AttributeError: module 'User.views' has no attribute 'User'
MyProject的/ urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('Home.urls')),
url(r'^Application', include('Application.urls')),
url(r'^Login', include('Login.urls')),
url(r'^User', include('User.urls')),
]
用户/ urls.py
from django.conf.urls import url, include
from User import views
urlpatterns = [
url(r'Home/', views.User, name='Home'),
url(r'Matrices/', views.User, name='Matrices'),
]
User / views.py
from django.shortcuts import render
def Home(request):
return render(request, 'Home.html')
def Matrices(request):
return render(request, 'Matrices.html')
如果我从MyProjects / urls.py删除“url(r'^ User',include('User.urls'))”,一切正常(但当然我无法访问来自User app的网址)。所有其他应用只有一个“网址”,只有用户应用有多个网址。这就是问题所在吗?
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
您的urlpatterns需要使用views.py函数,例如
urlpatterns = [
url(r'Home/$', views.Home, name='Home'),
url(r'Matrices/$', views.Matrices, name='Matrices'),
]
答案 1 :(得分:1)
user / views.py没有方法User,你只有两个方法HOME和MATRICES,并且你试图调用名为User的方法做这个`
url(r'Matrices/', views.User, name='Matrices')
要解决您的问题,您需要调用user / views.py HOME或MATRICES中的可用方法
url(r'ThisIsTheNameOfTheUrlNotTheMethod/', views.Home, name='Home'),
或
url(r'matrices/', views.Matrices, name='Matrices'),
答案 2 :(得分:0)
url(r'Home/', views.User, name='Home')
从上述代码中,尝试删除逗号和单词views.user
之间的空格。这解决了我的问题。
插图:代码应该看起来像;
url(r'Home/',views.User, name='Home')
。 comma
和views.user
之间没有空格
答案 3 :(得分:0)
在您的User/urls.py
替换以下行:
from User import views
带有以下行:
from . import views