Very elementary TypeError in Django - 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')),

Related

onw of my two app is not working in django

Below is my code. In my hello_world project there is two app pages. one is home page and another is profile page. home page is working fine, but profile page is showing error.
hello_world urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home_page.urls',)),
path('profile_page',include('profile_page.urls',))
]
home page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home page'),
]
home page views.py
from django.http import HttpResponse
def home(request):
return HttpResponse('home page')
profile page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('profile_page',views.profile,name='profile page'),
]
profile page views.py
from django.http import HttpResponse
def profile(request):
return HttpResponse('profile page')
You need to use redirect method and include view name space as an argument..
Find the updated code:
from django.http import HttpResponse
def profile(request):
return redirect('profile page')
Don't forgot to import redirect...
The first argument of the path() function, i.e. the 'route' argument, must end with a forward slash.
path('profile_page/',include('profile_page.urls',))
Notice the forward slash at the end of the first argument, 'profile_page/'.
Your URLconf is not matching the expected pattern because of the missing slash.

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

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.

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

URLconf does not appear to have any patterns in it/Circular import error

I am following a tutorial found in the Django documentation and upon attempting to map a view to a URL I received the following error:
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'pollSite.urls' d
oes not appear to have any patterns in it. If you see valid patterns in the file th
en the issue is probably caused by a circular import
. I have a pollSite project and a poll app.
pollSite/pollSite/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/', include('polls.urls')),
]
pollSite/poll:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
I thought I may have mistyped something so I went back and copied the code from the docs and pasted it directly into my editor and still got the same error. I'm not sure what a circular import is but I am also working with virtualenv for the first time and am not sure if that could be causing this. Any suggestions?
Tutorial incase anyone was interested: https://docs.djangoproject.com/en/2.2/intro/tutorial01/
Your app is called "poll", not "polls". So you have to include it by that name:
path('polls/', include('poll.urls')),

TypeError: view must be a callable or a list/tuple in the case of include()

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().
Urls. py code:-
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:-
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
In 1.10, you can no longer pass import paths to url(), you need to pass the actual view function:
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
Replace your admin url pattern with this
url(r'^admin/', include(admin.site.urls))
So your urls.py becomes :
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).
For Django 1.11.2
In the main urls.py write :
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:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_home),
]
Answer is in 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'))
Just to complement the answer from #knbk, we could use the template below:
as is in 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:
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.
You need to pass actual view function
from posts.views import post_home
urlpatterns = [
...
url(r'^posts/$', post_home),
]
This works fine!
You can have a read at URL Dispatcher Django
and here Common Reguler Expressions Django URLs