TypeError('view must be a callable or a list/tuple in the case of include().') - django

I used the Django's version is 1.10.4 and was seeing in my file mysite/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/',include(admin.site.urls)),
url(r'^website/$',"website.views.first_page"),
]
and views.py in mysite/mysite/
# -*- coding: utf-8 -*-
from django.http import HttpResponse
def first_page(request):
return HttpResponse("<p>hello Django</p>")
Did these settings but I always have the current error.Help me.

String reference is deprecated in Django 1.10.
So, Django 1.10 no longer allows you to specify views as a string in your URL patterns.
You can no longer pass import paths to url(), you need to pass the actual view function.
The solution is to update your urls.py to include the view callable.
This means that you have to import the view in your urls.py.
Use these urls.py instead :
from django.conf.urls import url,include
from django.contrib import admin
from mysite.views import first_page
urlpatterns = [
url(r'^admin/',include(admin.site.urls)),
url(r'^website/$',first_page)
]

Related

Django-filer problem when retrieve uploaded image in Django admin interface or any http|s request [duplicate]

I am using Django-Filer in my admin on a Django web project which is hosted in PythonAnywhere. I have gone through the installation instructions but I am having trouble accessing the canonical urls the Filer makes for each file. It seems I am being directed to an extended url that Filer.urls is not recognizing (the non-canonical part starts at /filer-public/; this is a directory that is being created and storing my files on the separate PythonAnywhere file directory).
Is there an error in my urls syntax? An error in my views.canonical? I am unsure of why plugging in the exact canonical url redirects me to this extended version of the url.
Python: 3.7
Django: 2.2
Canonical URL:
/filer/sharing/1560887480/39/
ERROR / DEBUGGING SCREEN
Page not found (404)
Request Method: GET
Request URL: http://www.mywebsite.com/filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg
Using the URLconf defined in mywebsitesite.urls, Django tried these URL patterns, in this order:
admin/
^filer/ sharing/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$ [name='canonical']
The current path, filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg, didn't match any of these.
APP URLS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/urls.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from django.conf.urls import url
from . import settings as filer_settings
from . import views
urlpatterns = [
url(
filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$', # flake8: noqa
views.canonical,
name='canonical'
),
]
APP VIEWS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/VIEWS.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
from .models import File
def canonical(request, uploaded_at, file_id):
"""
Redirect to the current url of a public file
"""
filer_file = get_object_or_404(File, pk=file_id, is_public=True)
if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
raise Http404('No %s matches the given query.' % File._meta.object_name)
return redirect(filer_file.url)
BASE URLS: /home/mywebsite/mywebsite/urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
from django.views.generic import TemplateView
from quotes.views import Register
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
path('', include('pages.urls')),
url(r'^filer/', include('filer.urls')),
]
BASE SETTINGS: /home/mywebsite/mywebsite/settings.py
FILER_CANONICAL_URL = 'sharing/'
I was able to get the canonical url to work by configuring the static and media root in my urls and settings files, as shown below.
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
url(r'^filer/', include('filer.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = os.environ.get('FILER_MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))
MEDIA_URL = '/home/mywebsite/media/'
I was able to solve this by not including the filer.urls but rather specifying the url pattern directly in my projects urls.py file.
Django 3.1.7
django-filer 2.1.2
/myproject/myproject/urls.py:
...
from filer import views as filer_views
from .settings import FILER_CANONICAL_URL
...
urlpatterns = [
url(r'^filer/'+FILER_CANONICAL_URL+r'/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
filer_views.canonical,
name='canonical'),
...,
]
/myproject/myproject/settings.py
...
FILER_CANONICAL_URL = 'somefolder/'
However I'm still not sure where the extra space was coming from in the first place - the filer urls.py and settings.py look fine as far as I can tell.

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.

ImportError: cannot import name views while running the server

I am getting following error and don´t know how to solve this:
from . import views
ImportError: cannot import name views
This are my scripts i am using:
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$',views.home,name='index'),
url(r'^admin/', admin.site.urls),]
views.py
from __future__ import unicode_literals
from django.http import HttpResponse
def index(request):
return HttpResponse("libaray management system")
Try to import like this
from your_app_name import views
I can't say, what is your project structure and where are your views.py and urls.py files. My guess, that you have some problems with how you import (check the absolute and relative import in python). In that case you can use import style from above code example.

How to fix this error - Not Found: /firstapp/ "GET /firstapp/ HTTP/1.1" 404 2065"

I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
firstapp
urls.py
from django.conf.urls import url
from . import views
urlpatterns={
url('r^$',views.index,name='index')
}
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<H2> Hey Everyone welcome to Django Tutorial! </H2>")
# Create your views here.
firstproject
urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/', include('firstapp.urls')),
]
I expect the output to be "Hey Everyone welcome to Django Tutorial!"
In your firstapp urls.py there is a mistake use [] instead of {}
second mistake in is your code url regex ('r^$') is wrong
change it to (r'^$')
in firstapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='index')
]

Not able to map a view with url

I am facing some problem while mapping a view with URL
Project name-Hero
app name-Movie
Hero/url.py:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'^',include('Movie.urls'))
]
Movie/urls.py:
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='Movie'),
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello friends ,Welcome to Django Project</h1>")
These are my urls and views configurations.I am getting a 404 page.
Please help me out.. Thanks
Try with below code, only changes in Hero/url.py
Hero/url.py
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'',include('Movie.urls')) # Do not add caret sign if further urls contains caret.
]