Error while integration a html template and url in django - django

I am using Django version 1.10.
Below is my urls.py(frontend),
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^webApp/', include('webApp.urls')),
url(r'^admin/', admin.site.urls),
url(r'^home/$', 'frontend.views.home', name='home'),
]
Below is my urls.py(webApp),
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And below is my views.py,
def home(request):
return render_to_response('home.html')
Here, frontend is my project name and webApp is my app name. And i have a home.html in my templates folder in frontend.
When I run,
python manage.py runserver 0.0.0.0:8000
I get the following error,
File "/root/frontend/frontend/urls.py", line 22, in <module>
url(r'^home/$', 'frontend.views.home', name='home'),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 85, in url
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()
I don't know what I am doing wrong... Any guidance in the same?

In the urlpatterns list you are not properly using the function url (you're passing a string as its second argument, but it - in this case - [..] must be a callable [..]).
So... just change 'frontend.views.home' to frontend.views.home (i.e. remove single quotes) and you should be fine.

Related

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

module 'guestbook.views' has no attribute 'index'

i make a startapp "guestbook" inside my django project.
here is the file list(guestbook)
__init__.py admin.py apps.py migrations models.py template tests.py urls.py views.py
guestbook/template
guestbook
guestbook/template/guestbook
index.html
i want to get the index render but i am facing error which is
File "C:\Users\_monster\Desktop\skill\django_frontend\backend\f_django\guestbook\urls.py", line 7, in <module>
path('', views.index, name='index')
AttributeError: module 'guestbook.views' has no attribute 'index'
here is my file setting
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# main url, folder url
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
path('guestbook/', include('guestbook.urls'))
]
guestbook/urls.py
from django.urls import path
# import everything from views
from . import views
urlpatterns = [
path('', views.index, name='index')
]
guestbook/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'guestbook/index.html')
so what is problem here ? the index.html are inside the folder
I had the same error. I Tried almost everything to solve the issue but couldnt.
I just changed the name of function index from views.py to index2(guestbook/views.py)in your case!! Then i replicated those changes to (guestbook/urls.py) as
urlpatterns = [
path('', views.index2, name='index')
]
It happened miraculously. And the console showed no errors anymore!! When i switched back again to whatever previously was, (that is to index from from index2) everything was running correctly

no module named search after adding project url

I've had the pleasure to work with somebody yesterday on the issue with my urls here Adding an additional template to an existing project errors out, but after trying everything suggested i'm still in the same situation.
My project is named mysite and my application is search.
It was suggested to add the following to my project urls.py
url(r'^search/', include('search.urls')),
When doing so I'm given the error ModuleNotFoundError: No module named 'search'.
My project urls.py is the following:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^search/$', FilterView.as_view(filterset_class=UserFilter, template_name='search/user_list.html'), name='search'),
url(r'^admin/', include(admin.site.urls)),
url(r'^search/', include('search.urls')),
]
I'm attempting to add the following to my app urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django_filters.views import FilterView
from mysite.search.filters import UserFilter
from . import views
urlpatterns = [
url(r'^results/$', views.results, name='results'),
]
I have an empty view for results defined as
def results(request):
return render(request, 'results.html')
When I try to add the following POST to my form for the results it gives me the error in the first post. When I have the results url in my app.urls.py
<form action = "{% url 'results' %}" form method = "POST">
This is what my current application structure looks like. Please help get me on the right track. Thank you.
Your search directory is in your mysite directory (the one that includes settings.py. That means you should include mysite.search.urls (just as you use mysite.search in your import and INSTALLED_APPS).
from mysite.search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('mysite.search.urls')),
]
If your search directory was in your project directory (the one that includes manage.py, then you would remove mysite from the import, include() and INSTALLED_APPS.
from search.filters import UserFilter
urlpatterns = [
...
url(r'^search/', include('search.urls')),
]

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')),

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