Django error:view must be a callable or a list/tuple in the case of include() - django

Well i'm new to Django and i'm following outdated course (its free obv) and ran up to this error. What can i change in my code>
Here are codes from views.py and urls.py from both folders:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("This is teh index view!")
#next one
from django.urls import include, path
from msg import views
urlpatterns = path('', r'^$',views.index,name = "index")
#next one
from django.contrib import admin
from django.urls import path
from msg.urls import urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('msg/', urlpatterns),
]
after trying to makemigrations i get this error :
TypeError: view must be a callable or a list/tuple in the case of include().

It's because your urlpatterns path() syntax is wrong see
path(route, view, kwargs=None, name=None)ΒΆ
urlpatterns = [path('',views.index,name = "index")]
You are using a regular expression in your path, that has been deprecated so you should pick a tutorial that uses that format.
The reason you get the error when running migrations is that the project starts when you run Manage.py and then the app starts immediately afterwards. The app start does some basic checks and then borks if your have an error in the URLs file.

Related

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 .Django 3.0.2

Project name : CRMPROJECT
App name: leads
I'm working on a CRM project With Django and I keep getting this error while trying to map my urls to views
django.core.exceptions.ImproperlyConfigured. The included URLconf does not appear to have any patterns in it
Below is the project/urls.py file
'''
from django.contrib import admin
from django.urls import path,include
from leads import views
urlpatterns = [
path('admin/', admin.site.urls),
path('leads/',include('leads.urls',namespace='leads')),
]
'''
Below is leads/urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('',views.home,name='home'),
]
And lastly my leads/views.py
'''
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'leads/base.html')
'''

I have configured every thing but django says page not found

can any one help me with this problem?
this is my blog/urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path("/testTemplate", template_testing, name="testtemplate"),
]
and this is my website/urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and this is blog/views.py:
from django.shortcuts import render
# Create your views here.
def template_testing(request):
return render(request, "blog/index.html", {})
every things looks OK but when I want to go to this url: 127.0.0.1:8000/testTemplate I get a page not found error. What is going wrong here?
please help me!
First of all, make sure that your test server is running by going to the directory containing manage.py. Then make sure you have typed:
python manage.py runserver
Once you've done this, note that the trouble with your script is that the method "template_testing" is not referencing the correct render file. Change the following lines:
def template_testing(request):
return render(request, "blog/index.html", {})
To:
def template_testing(request):
return render(request, "testTemplate", {})
the template names you mentioned in blog/urls.py and in blog/views.py are different
both must contain same template name.

How to fix 'The included URLconf 'gp.urls' does not appear to have any patterns in it'

when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.urls' 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.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work

Very elementary TypeError in Django

I'm following all of the instructions in this tutorial, time 2:26
https://www.youtube.com/watch?v=gqRLPx4ZeSw&t=3s
and I cannot get the expected result. The TypeError I'm getting is
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
File: urls.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'),
# url(r'^posts/$', '<appname>.views.post_home'),
]
File: views.py
from django.shortcuts import render
from django.http import HttpResponse
def post_home(request):
return HttpResponse("<h1>Hello</h1>")
And here are the relevant screenshots, however I cannot post them because the computer thinks that they're code. Because it thinks they're code when I hit cntrl k the screenshots go away, but if I do not hit cntrl k, then I cannot post the thread.
You should do this for your code to work:
from posts import views as posts_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', posts_views.post_home),
]
But it's best you use include to append the app urls, you have to create an
urls.py in your posts app.
from django.conf.urls import url, include
url(r'^posts/', include('posts.urls')),

Django Name Error

I have the following code in my urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^testModule/',include(testModule.urls)),
url(r'^admin/', admin.site.urls),
]
and testModule is my app name which includes :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
And my views.py is
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
# Create your views here.
However, i get the following error while running the server:
line 20: url(r'^testModule/',include(testModule.urls)),
NameError: name 'testModule' is not defined
You haven't imported testModule in your main urls.
Had the same problem but solved it like this:
add "import testModule" to your urls.py
add "from django.conf.urls import url" to your urls.py in the app directory, in this case testModule
Once done you will get a "Page not found at/" error when you reload 127.0.0.1:8000. This is bacause the url to your app is actually at 127.0.0.1:8000/testModule