ERROR:- as_view() takes 1 positional argument but 2 were given - django

After running the server i am getting error as as_view() takes 1 positional argument but 2 were given please have a look on below code and suggest me.
views.py
from django.views.generic import View
import json
class JsonCBV(View):
def get(self,request,*args, **kwargs):
emp_data=
{'eno':100,'ename':'pankhu','esal':300000,'eaddr':'pune'}
return JsonResponse(emp_data)
urls.py
from django.contrib import admin
from django.urls import path
from testapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('jsonapi3/', views.JsonCBV.as_view),
]
test.py
import requests
BASE_URL='http://127.0.0.1:8000/'
ENDPOINT='jsonapi3'
resp =requests.get(BASE_URL+ENDPOINT)
data=resp.json()
print('Data from django application')
print('#'*50)
print('Employee number:',data['eno'])
print('Employee name:',data['ename'])
print('Employee salary:',data['esal'])
print('Employee address:',data['eaddr'])

in your urls you have to write parentheses after .as_view()
path('jsonapi3/', views.JsonCBV.as_view())

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.

Pass a file path as a URL parameter in Django

I'm using Django to create a webapp. When a user press on a certain button, it needs to pass a file path as parameter and a string parameter to one of my views. I can't simply use the parameter in the URL since the path contains several '/'. The way I have it setup right now is as follows:
parameters.py
class FilePathConverter:
regex = '^[/]'
def to_python(self, value):
value=str(value)
return value.replace("?", "/")
def to_url(self, value):
value=str(value)
return value.replace("/", "?")
urls.py
from django.urls import path
from . import views
from django.contrib import admin
from django.views import generic
from django.urls import path, register_converter
from . import converters, views
register_converter(converters.FilePathConverter, 'filepath')
urlpatterns = [
path('', views.index, name='webpanel-index'),
path('controlserver/<filepath:server_path>/<str:control>', views.index, name='controlserver'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Server
from django.contrib.auth.decorators import login_required
import subprocess
def controlserver(request, server_path, control):
if request.POST:
subprocess.call(['bash', server_path, control])
return render(request, 'index.html')
However, with this method, I get this error:
Reverse for 'controlserver' with keyword arguments '{'server_path': 'rien/', 'control': 'start'}' not found. 1 pattern(s) tried: ['controlserver/(?P<server_path>[^/]+)/(?P<control>[^/]+)$']
you can use Slug to resolve this patterns by :
from django.utils.text import slugify
path('controlserver/use slug .....', views.index, name='controlserver'),
but you need to put slug at views and templates So check this list of slug and pk :
https://github.com/salah-cpu/migration/blob/master/PATH_slug_pk

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.

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