Class-based TemplateView are not working, help me find the error
from django.shortcuts import render
from django.views.generic.base import View,TemplateView
from django.http import HttpResponse
# Create your views here.
class home(TemplateView):
template_name = 'index.html'
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['injectme'] = 'injected'
return context
url.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from basicapp import views
urlpatterns = [
url(r'^$',views.home.as_view(),name = 'home'),
path('admin/', admin.site.urls),
]
TypeError at /
expected str, bytes or os.PathLike object, not tuple
Request Method: GET
Request URL: http://127.0.0.1:8000/ Django Version: 2.2 Exception
Type: TypeError Exception Value: expected str, bytes or os.PathLike
object, not tuple Exception
Location: /anaconda3/envs/mydjangoEnv/lib/python3.7/posixpath.py in
join, line 80 Python
Executable: /anaconda3/envs/mydjangoEnv/bin/python Python
Version: 3.7.3 Python Path:
['/Users/faiq/Desktop/exercise/CBV/advance',
'/anaconda3/envs/mydjangoEnv/lib/python37.zip',
'/anaconda3/envs/mydjangoEnv/lib/python3.7',
'/anaconda3/envs/mydjangoEnv/lib/python3.7/lib-dynload',
'/anaconda3/envs/mydjangoEnv/lib/python3.7/site-packages'] Server
time: Wed, 8 May 2019 23:02:58 +0000
Related
I am hitting a conflict with a prior configuration and not sure how to get around the error.
error:
Exception Value: module 'userarea.views' has no attribute 'home_view'
from project urls.py:
$ cat exchange/urls.py
from django.contrib import admin
from django.urls import path, include
from pages.views import home_view, contact_view, about_view, user_view
#from products.views import product_detail_view
from userdash.views import userdash_detail_view, userdash_create_view
from django.conf.urls import include
from django.urls import path
from register import views as v
from pages import views
from userarea import views
urlpatterns = [
path('', views.home_view, name='home'),
path('', include("django.contrib.auth.urls")),
path('admin/', admin.site.urls),
path("register/", v.register, name="register"),
path('userdash/', userdash_detail_view),
path('contact/', contact_view),
path('', include("userarea.urls")),
path('create/', userdash_create_view),
path('about/', about_view),
path('user/', user_view),
]
I either get this error or problem with it trying to access my default.
But unable to find a work around.
from app views.py
$ cat userarea/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def uindex(response):
return HttpResponse("<h1>New user dashboard area</h1>")
app urls.py:
$ cat userarea/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("uindex/", views.uindex, name="user index"),
]
How do I get my new/2nd project to not conflict with my previous app and load it's own index page in it's directory?
full debug error:
AttributeError at /uindex
module 'userarea.views' has no attribute 'home_view'
Request Method: GET
Request URL: http://192.168.42.13:8080/uindex
Django Version: 2.2.7
Exception Type: AttributeError
Exception Value:
module 'userarea.views' has no attribute 'home_view'
Exception Location: ./exchange/urls.py in <module>, line 28
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.7.3
Python Path:
['.',
'',
'/home/kermit/Env/secret/lib/python37.zip',
'/home/kermit/Env/secret/lib/python3.7',
'/home/kermit/Env/secret/lib/python3.7/lib-dynload',
'/usr/lib/python3.7',
'/home/kermit/Env/secret/lib/python3.7/site-packages']
Note updated urlpatterns twice and errors
new update:
This is what it appears to be calling, but I can't figure out how to differentiate between the two calls.
$ cat pages/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_view(request, *args, **kwargs):
return render(request, "home.html",{})
def contact_view(request, *args, **kwargs):
return render(request, "contact.html",{})
def about_view(*args, **kwargs):
return HttpResponse('<h1>About Page</h1>')
def user_view(request, *args, **kwargs):
my_context = {
"my_text": "This is my context",
"my_number": "123",
"my_list": [1121, 1212, 3423, "abc"]
}
print(args, kwargs)
print(request.user)
return render(request, "user.html", my_context)
Big UPDATE (changes made above also):
I create a brand spanking new project and used the exact same configuration and it works find.
(Env) piggy#tuna:~/www/src/exchange2 $ cat userdash/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("userdash/", views.userdash, name="user dash"),
]
(Env) piggy#tuna:~/www/src/exchange2 $ cat exchange2/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("userdash.urls")),
]
(Env) piggy#tuna:~/www/src/exchange2 $ cat userdash/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(response):
return HttpResponse("<h1>Hello World!</h1>")
def userdash(response):
return HttpResponse("<h1>User Dashboard!</h1>")
why does my include() method of creating directories want to conflict with a standard path()?
from
irc.freenode.net #django user <GinFuyou>
.
from userarea import views as userarea_views
the key was
userarea_views
Trying a simple request:
urls.py
from django.conf.urls import url
from django.urls import include, path
from rest_framework import routers
from django.http import HttpResponse
from rest_framework.urlpatterns import format_suffix_patterns
from .public_views import NavigationBar
router = routers.DefaultRouter()
router.register(r'navbar', NavigationBar, basename="NavigationBar")
urlpatterns = [
path('', include(router.urls))
]
urlpatterns = format_suffix_patterns(urlpatterns)
public_views.py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework.throttling import UserRateThrottle
from rest_framework.decorators import api_view, throttle_classes
from . view_utils import *
class OncePerDayUserThrottle(UserRateThrottle):
rate = '1/day'
class NavigationBar(APIView):
"""
obtain up to date navigation bar (or side menu navigation) hierarchy.
"""
permission_classes = ([AllowAny])
def get(self, request, format=None):
"""
get user addresses
"""
return Response("this is a good response")
def get_extra_actions(cls):
return []
When I API call the /v1/navbar or /v1/navbar/ endpoints (I do have my main urls.py lead all /v1/ traffic to another dedicated urls.py), I am getting the following error:
AttributeError at /v1/navbar
type object 'NavigationBar' has no attribute 'get_extra_actions'
Request Method: GET
Request URL: http://web/v1/navbar
Django Version: 2.1
Exception Type: AttributeError
Exception Value:
type object 'NavigationBar' has no attribute 'get_extra_actions'
Exception Location: /usr/local/lib/python3.6/site-packages/rest_framework/routers.py in get_routes, line 200
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.6.8
Python Path:
['.',
'',
'/usr/local/lib/python36.zip',
'/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages']
Server time: Tue, 2 Jul 2019 17:12:27 +0000
I would appreciate any pointers. Also, I fail to understand why the error message includes a Request URL: http://web/v1/navbar indication when web is not part of the URL I'm using. Where is web coming from??? I just use /v1/navbar/ to hit the endpoint.
There are two things wrong here. First, routers are for viewsets, not simple views. Secondly, with a class based view you need to call it via its as_view() method in the urlconf. So get rid of that router stuff and just do:
urlpatterns = [
path(r'navbar', NavigationBar.as_view(), name="NavigationBar")
]
Note, now you're not using a router, you don't need that get_extra_actions method at all.
Trying a simple request:
urls.py
from django.conf.urls import url
from django.urls import include, path
from rest_framework import routers
from django.http import HttpResponse
from rest_framework.urlpatterns import format_suffix_patterns
from .public_views import NavigationBar
router = routers.DefaultRouter()
router.register(r'navbar', NavigationBar, basename="NavigationBar")
urlpatterns = [
path('', include(router.urls))
]
urlpatterns = format_suffix_patterns(urlpatterns)
public_views.py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework.throttling import UserRateThrottle
from rest_framework.decorators import api_view, throttle_classes
from . view_utils import *
class OncePerDayUserThrottle(UserRateThrottle):
rate = '1/day'
class NavigationBar(APIView):
"""
obtain up to date navigation bar (or side menu navigation) hierarchy.
"""
permission_classes = ([AllowAny])
def get(self, request, format=None):
"""
get user addresses
"""
return Response("this is a good response")
def get_extra_actions(cls):
return []
When I API call the /v1/navbar or /v1/navbar/ endpoints (I do have my main urls.py lead all /v1/ traffic to another dedicated urls.py), I am getting the following error:
AttributeError at /v1/navbar
type object 'NavigationBar' has no attribute 'get_extra_actions'
Request Method: GET
Request URL: http://web/v1/navbar
Django Version: 2.1
Exception Type: AttributeError
Exception Value:
type object 'NavigationBar' has no attribute 'get_extra_actions'
Exception Location: /usr/local/lib/python3.6/site-packages/rest_framework/routers.py in get_routes, line 200
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.6.8
Python Path:
['.',
'',
'/usr/local/lib/python36.zip',
'/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages']
Server time: Tue, 2 Jul 2019 17:12:27 +0000
I would appreciate any pointers. Also, I fail to understand why the error message includes a Request URL: http://web/v1/navbar indication when web is not part of the URL I'm using. Where is web coming from??? I just use /v1/navbar/ to hit the endpoint.
There are two things wrong here. First, routers are for viewsets, not simple views. Secondly, with a class based view you need to call it via its as_view() method in the urlconf. So get rid of that router stuff and just do:
urlpatterns = [
path(r'navbar', NavigationBar.as_view(), name="NavigationBar")
]
Note, now you're not using a router, you don't need that get_extra_actions method at all.
I want to use re_path from django.urls, django doc says I can use this from django version 2,
From the error I came to know that I am using django version 2.0.6.
But I am not able to re_path
ImportError at /
cannot import name 're_path'
Request Method: GET Request URL: http://djangosite.com/ Django Version:
2.0.6 Exception Type: ImportError Exception Value:
cannot import name 're_path'
Exception Location: /home/sugushiva/myproject/filope/blogs/urls.py in
, line 1 Python Executable: /usr/bin/python3
In main urls.py
from django.contrib import admin
from django.urls import path, re_path,include
urlpatterns= [
path('admin/',admin.site.urls),
re_path('^$', include('blogs.urls'))
]
in blogs.urls
from django.db import re_path
from .models import blogindex
urlpatterns = [
re_path('^$',blogindex)
]
You are importing the wrong path in blogs/urls.py.
from django.db import re_path
Should instead be:
from django.urls import re_path
I downloaded the code from the following link and stored it in the location below,
http://django-rest-interface.googlecode.com/svn/trunk/ django-rest-interface
Location
c:/Python27/Djangoprojects/django_restapi
Project Location
c:/Python27/Djangoprojects/mysite/polls
URLS.py
from django.conf.urls.defaults import *
from polls.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/$',index),
(r'^polls/(?P<poll_id>\d+)/$',detail),
(r'^polls/(?P<poll_id>\d+)/results/$',results),
(r'^polls/(?P<poll_id>\d+)/vote/$',vote),
(r'^admin/', include(admin.site.urls)),
(r'^xml/polls/(.*?)/?$',xml_poll_resource),
)
views.py
from django_restapi.model_resource import Collection
from django_restapi.responder import XMLResponder
from django_restapi.responder import *
from django_restapi_tests.polls.models import Poll, Choice
xml_poll_resource = Collection(
queryset = Poll.objects.all(),
permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),
responder = XMLResponder(paginate_by = 10)
)
I get the following error when I try the URL specified below,
Error:
ImportError at /xml/polls/
No module named django_restapi.model_resource
Request Method:
GET
Request URL:
http://127.0.0.1:8000/xml/polls/
Django Version:
1.3.1
Exception Type:
ImportError
Exception Value:
No module named django_restapi.model_resource
Exception Location:
C:\Python27\Djangoprojects\mysite..\mysite\polls\views.py in , line 1
Python Executable:
C:\Python27\python.exe
Python Version:
2.7.2
Python Path:
['C:\Python27\Djangoprojects\mysite',
'C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg',
'C:\Python27\lib\site-packages\django_db_log-2.2.1-py2.7.egg',
'C:\Python27',
'c:\Python27\lib\site-packages\django\bin\django-admin.py',
'c:\mysql',
'c:\pythonpath\djangoprojects\django_restapi',
'C:\Windows\system32\python27.zip',
'C:\Python27\DLLs',
'C:\Python27\lib',
'C:\Python27\lib\plat-win',
'C:\Python27\lib\lib-tk',
'C:\Python27\lib\site-packages',
'C:\Python27\lib\site-packages\wx-2.8-msw-unicode']
Server time:
Thu, 12 Jul 2012 22:31:04 -0400
How do I resolver this error?
As from your code, Polls directory should be inside django_restapi and you should put parent of django_restapi in python path.
Did you check the setting.py file?