I am new to django and python. During url mapping to views i am getting following error: TypeError: view must be a callable or a list/tuple in the case of include().
我是django和python的新手。在到视图的url映射过程中,我得到了以下错误:TypeError: view必须是可调用的,如果是include(),则必须是列表/元组。
Urls. py code:-
url。py代码:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
] # is a function in view.
views.py code:-
的观点。py代码:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
#function based views
def post_home(request):
response = "<h1>Success</h1>"
return HttpResponse(response)
Traceback
回溯
6 个解决方案
#1
26
In 1.10, you can no longer pass import paths to url()
, you need to pass the actual view function:
在1.10中,不能再将导入路径传递给url(),需要传递实际的视图函数:
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
#2
2
Replace your admin url pattern with this
用这个替换您的管理url模式
url(r'^admin/', include(admin.site.urls))
So your urls.py becomes :
所以你的url。py就变成:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
]
admin urls are callable by include (before 1.9).
admin url可以通过include调用(在1.9之前)。
#3
1
For Django 1.11.2
In the main urls.py write :
对于主要url中的Django 1.11.2。py写:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include("Post.urls")),
]
And in the appname/urls.py file write:
和浏览器名称/ url。py文件编写:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_home),
]
#4
0
Answer is in project-dir/urls.py
答案是project-dir / urls . py
Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
#5
0
Just to complement the answer from @knbk, we could use the template below:
为了补充@knbk的答案,我们可以使用下面的模板:
as is in 1.9:
是在1.9:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls), #it's not allowed to use the include() in the admin.urls
url(r'^posts/$', include(posts.views.post_home),
]
as should be in 1.10:
如1.10所示:
from your_project_django.your_app_django.view import name_of_your_view
urlpatterns = [
...
url(r'^name_of_the_view/$', name_of_the_view),
]
Remember to create in your_app_django >> views.py the function to render your view.
请记住在your_app_django >>视图中创建。函数的作用是:渲染视图。
#6
0
You need to pass actual view function
您需要传递实际的视图函数
from posts.views import post_home
从职位。视图导入post_home
urlpatterns = [ ... url(r'^posts/$', post_home), ]
urlpattern =[…url(r / $ ^帖子,post_home),)
This works fine! You can have a read at URL Dispatcher Django and here Common Reguler Expressions Django URLs
这个没问题!您可以在URL分派器Django和这里常见的正则表达式Django URL上进行读取
#1
26
In 1.10, you can no longer pass import paths to url()
, you need to pass the actual view function:
在1.10中,不能再将导入路径传递给url(),需要传递实际的视图函数:
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
#2
2
Replace your admin url pattern with this
用这个替换您的管理url模式
url(r'^admin/', include(admin.site.urls))
So your urls.py becomes :
所以你的url。py就变成:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/$', "posts.views.post_home"), #posts is module and post_home
]
admin urls are callable by include (before 1.9).
admin url可以通过include调用(在1.9之前)。
#3
1
For Django 1.11.2
In the main urls.py write :
对于主要url中的Django 1.11.2。py写:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include("Post.urls")),
]
And in the appname/urls.py file write:
和浏览器名称/ url。py文件编写:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_home),
]
#4
0
Answer is in project-dir/urls.py
答案是project-dir / urls . py
Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
#5
0
Just to complement the answer from @knbk, we could use the template below:
为了补充@knbk的答案,我们可以使用下面的模板:
as is in 1.9:
是在1.9:
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls), #it's not allowed to use the include() in the admin.urls
url(r'^posts/$', include(posts.views.post_home),
]
as should be in 1.10:
如1.10所示:
from your_project_django.your_app_django.view import name_of_your_view
urlpatterns = [
...
url(r'^name_of_the_view/$', name_of_the_view),
]
Remember to create in your_app_django >> views.py the function to render your view.
请记住在your_app_django >>视图中创建。函数的作用是:渲染视图。
#6
0
You need to pass actual view function
您需要传递实际的视图函数
from posts.views import post_home
从职位。视图导入post_home
urlpatterns = [ ... url(r'^posts/$', post_home), ]
urlpattern =[…url(r / $ ^帖子,post_home),)
This works fine! You can have a read at URL Dispatcher Django and here Common Reguler Expressions Django URLs
这个没问题!您可以在URL分派器Django和这里常见的正则表达式Django URL上进行读取