handlers error in django-piston - python

i got a this error in my django project when i implement another handlers for my django-piston.
Request Method: GET
Request URL: http://127.0.0.1:8000/api/getdata/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'app',
'api',
'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
250. for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
279. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
274. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/agileone/workspace/proj/api/urls.py" in <module>
15. devdata_handler = Resource(DevDataHandler)
File "/usr/local/lib/python2.7/dist-packages/django_piston-0.3dev-py2.7.egg/piston/resource.py" in __init__
36. self.handler = handler()
Exception Type: TypeError at /api/getdata/
Exception Value: DevDataHandler() takes exactly 1 argument (0 given)
i don't know what is the reason why i got these.
this is my api/urls.py
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import *
from django.http import HttpResponse
from piston.handler import BaseHandler, AnonymousBaseHandler
class CsrfExemptResource(Resource):
def __init__(self, handler, authentication=None):
super(CsrfExemptResource,self).__init__(handler,authentication)
self.csrf_exempt=getattr(self.handler,'csrf_exempt',True)
data_handler = CsrfExemptResource(DataHandler)
devdata_handler = Resource(DevDataHandler)
urlpatterns=patterns('',
url(r'^getdata/$', data_handler, {'emitter_format': 'ext-json'}),
url(r'^getdevdata/$', devdata_handler, {'emitter_format': 'ext-json'})
)
and this is my api/handlers.py
from django.utils import simplejson
from piston.handler import BaseHandler, AnonymousBaseHandler
from app.models import *
from django.db.models import *
from piston.utils import rc, require_mime, require_extended, validate
import datetime
class DataHandler(BaseHandler):
allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
fields = ('title')
model = Data
def read(self, request):
data = {"msg":"Hello world"}
return data
def DevDataHandler(BaseHandler):
allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
fields = ('title')
model = Data
def read(self, request):
data = {"msg":"Hi world"}
return data
it works if i did not include the devdata_handler = Resource(DevDataHandler) but i really need it...
do anyone can help me to solve my case?
thanks in advance

Handlers are classes, not functions. Change:
def DevDataHandler(BaseHandler):
to:
class DevDataHandler(BaseHandler):

Related

SyntaxError at /rango/,invalid syntax

I am following tangowithdjango django tutorial.While I am running http://127.0.0.1/rango I am getting following error.
the error is
Django Version: 1.8.5
Exception Type: SyntaxError
Exception Value: invalid syntax (views.py, line 15)
Exception Location: /root/work/tango_with_django_project/rang/urls.py in <module>, line 2
My urls.py is
from django.conf.urls import patterns,url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^category/(?P<slug>[\w\-]+)/$', views.category, name='category'),)
and views.py is
from django.shortcuts import render
from rango.models import Category,Page
from django.http import HttpResponse
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict={'categories':category_list}
return render(request, 'rango/index.html', context_dict)
def about(request):
return HttpResponse(" <a href='/rango/'>go back</a>")
def category(request,slug):
context_dict={}
try:
my_category = Category.objects.get(slug=category_name_slug)
context_dict['category_name'] = my_category.name
pages = Page.objects.filter(category=my_category)
context_dict['pages'] = pages
context_dict['category'] = my_category
except Category.DoesNotExist:
pass
return render(request, 'rango/category.html', context_dict)
Traceback is
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/rango/about/
Django Version: 1.8.5
Python Version: 2.7.9
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rango']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers /base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django /core/urlresolvers.py" in resolve
365. for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
401. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django /core/urlresolvers.py" in urlconf_module
395. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/root/work/tango_with_django_project/tango_with_django_project/urls.py" in <module>
23. url(r'^rango/',include('rango.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/root/work/tango_with_django_project/rango/urls.py" in <module>
2. from . import views
Exception Type: SyntaxError at /rango/about/
Exception Value: invalid syntax (views.py, line 15)
Please help me.Thanks in advance....
Your identation is wrong, look in:
# Retrieve all of the associated pages.
# Note that filter returns >= 1 model instance.
pages = Page.objects.filter(category=category)
# Adds our results list to the template context under name pages.
context_dict['pages'] = pages
# We also add the category object from the database to the context dictionary.
# We'll use this in the template to verify that the category exists.
context_dict['category'] = category
Now you have an indentation error here:
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict={'categories':category_list}
return render(request, 'rango/index.html', context_dict)

Python http_client error [duplicate]

I am trying to create a simple API using django rest framework. In the view i have the following code.
from django.shortcuts import render
from moviestash.models import Movie
from moviestash.serializer import MovieSerializer
from rest_framework import generics
#List all movies and add movies
class MovieList(generics.ListCreateAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
#Get a movie and delete a movie
class MovieDetail(generics.RetrieveDestroyAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
when i run the server and try to go to any url i get the following error.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.6
Python Version: 2.7.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'moviestash',
'south',
'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\handlers\base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\movies_api\urls.py" in <module>
10. url(r'^movies/', include('moviestash.urls')),
File "N:\Python\venvs\rest_api\lib\site-packages\django\conf\urls\__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\moviestash\urls.py" in <module>
3. from . import views
File "N:\Python\movies_api\moviestash\views.py" in <module>
4. from rest_framework import generics
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\generics.py" in <module>
8. from rest_framework import views, mixins
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\views.py" in <module>
14. from rest_framework.response import Response
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\response.py" in <module>
8. from django.utils.six.moves.http_client import responses
Exception Type: ImportError at /
Exception Value: No module named http_client
When i go into the django shell and i can perform the following import with no issue from django.utils.six.moves import http_client. Also after i import http_client i also performed a dir(http_client) and i can see the responses object, but for some reason when i try to import using from django.utils.six.moves.http_client import responses i get an ImportError: No module named http_client. This is very frustrating to say the least.
It looks like you are hitting issue 2969. It should work if you upgrade from Django 1.6 to 1.6.11. However, please note that 1.6 is now end of life and does not receive security fixes, so ideally you should upgrade to the latest supported version of Django or the latest LTS.

No module named http_client error when trying to run django with django rest framework

I am trying to create a simple API using django rest framework. In the view i have the following code.
from django.shortcuts import render
from moviestash.models import Movie
from moviestash.serializer import MovieSerializer
from rest_framework import generics
#List all movies and add movies
class MovieList(generics.ListCreateAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
#Get a movie and delete a movie
class MovieDetail(generics.RetrieveDestroyAPIView):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
when i run the server and try to go to any url i get the following error.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.6
Python Version: 2.7.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'moviestash',
'south',
'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\handlers\base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\movies_api\urls.py" in <module>
10. url(r'^movies/', include('moviestash.urls')),
File "N:\Python\venvs\rest_api\lib\site-packages\django\conf\urls\__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "N:\Python\venvs\rest_api\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "N:\Python\movies_api\moviestash\urls.py" in <module>
3. from . import views
File "N:\Python\movies_api\moviestash\views.py" in <module>
4. from rest_framework import generics
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\generics.py" in <module>
8. from rest_framework import views, mixins
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\views.py" in <module>
14. from rest_framework.response import Response
File "N:\Python\venvs\rest_api\lib\site-packages\rest_framework\response.py" in <module>
8. from django.utils.six.moves.http_client import responses
Exception Type: ImportError at /
Exception Value: No module named http_client
When i go into the django shell and i can perform the following import with no issue from django.utils.six.moves import http_client. Also after i import http_client i also performed a dir(http_client) and i can see the responses object, but for some reason when i try to import using from django.utils.six.moves.http_client import responses i get an ImportError: No module named http_client. This is very frustrating to say the least.
It looks like you are hitting issue 2969. It should work if you upgrade from Django 1.6 to 1.6.11. However, please note that 1.6 is now end of life and does not receive security fixes, so ideally you should upgrade to the latest supported version of Django or the latest LTS.

ImportError at /articles/api/article: No module named api(TastyPie)

I'm making a Django website and I downloaded the Tastypie API. But when I try to run my site on (localhost)/articles/api/article, I get this error:
ImportError at /articles/api/article: No module named api
Here is my api.py file:
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from models import Article
class ArticleResource(ModelResource):
class Meta:
queryset = Article.objects.all()
resource_name = 'article'
and my urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from api import ArticleResource
article_resource = ArticleResource()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/login/$', 'django_test.views.login'),
url(r'^accounts/auth/$', 'django_test.views.auth_view'),
url(r'^accounts/loggedin/$', 'django_test.views.loggedin'),
url(r'^accounts/invalid/$', 'django_test.views.invalid_login'),
url(r'^accounts/logout/$', 'django_test.views.logout'),
url(r'^accounts/register/$', 'django_test.views.register_user'),
url(r'^accounts/register_success/$', 'django_test.views.register_success'),
url(r'^articles/all/$', 'article.views.articles'),
url(r'^articles/create/$', 'article.views.create'),
url(r'^articles/get/(?P<article_id>\d+)/$', 'article.views.article'),
url(r'^articles/like/(?P<article_id>\d+)/$', 'article.views.like_article'),
url(r'^articles/add_comment/(?P<article_id>\d+)/$', 'article.views.add_comment'),
url(r'^articles/search/', 'article.views.search_titles'),
url(r'^articles/api/article', include(article_resource.urls)),
)
This is my traceback:
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/articles/api/article/
Django Version: 1.7.4
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.comments',
'article',
'tastypie')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/handlers/base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in resolve
343. for pattern in self.url_patterns:
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in url_patterns
372. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/deanna/django-deanna/local/lib/python2.7/site-packages/Django-1.7.4-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
366. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/deanna/django_test/django_test/urls.py" in <module>
3. from api import ArticleResource
Exception Type: ImportError at /articles/api/article/
Exception Value: No module named api
I don't know what I'm doing that's getting this error. I'd appreciate any help I can get. Thank You.

django 'Commande' object has no attribute '__name_

Hi stackoverflow people,
In my project i try to code a view which will manage am external api which will be used to fetch some datas, present them and store them in a database.
When i try to access to my view, i encounter the following error
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/commande/recherche
Django Version: 1.7.1
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'commands')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
87. response = middleware_method(request)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/middleware/common.py" in process_request
72. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in is_valid_path
619. resolve(path, urlconf)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
494. return get_resolver(urlconf).resolve(path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
345. sub_match = pattern.resolve(new_path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
345. sub_match = pattern.resolve(new_path)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in resolve
224. return ResolverMatch(self.callback, args, kwargs, self.name)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in callback
231. self._callback = get_callable(self._callback_str)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/functools.py" in wrapper
434. result = user_function(*args, **kwds)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/core/urlresolvers.py" in get_callable
97. mod = import_module(mod_name)
File "/home/user/.virtualenvs/commands-project/lib/python3.4/importlib/__init__.py" in import_module
109. return _bootstrap._gcd_import(name[level:], package, level)
File "/home/user/workspace/Python/commands-project/project/commands/views.py" in <module>
6. from .form import CommandesForm, CommandeForm
File "/home/user/workspace/Python/commands-project/project/commands/form.py" in <module>
11. class CommandeForm(forms.ModelForm):
File "/home/user/.virtualenvs/commands-project/lib/python3.4/site-packages/django/forms/models.py" in __new__
293. opts.model.__name__)
Exception Type: AttributeError at /commande/recherche
Exception Value: 'Commande' object has no attribute '__name__'
My models.py :
from django.db import models
class Client(models.Model):
client = models.IntegerField(null=True)
class Commandes(models.Model):
date_debut = models.DateField()
date_fin = models.DateField()
id_groups = models.CharField(max_length=100)
id_client = models.ForeignKey(Client)
class Commande(models.Model):
id_flux = models.CharField(max_length=100, null=True, blank=True)
id_commande = models.CharField(max_length=100, null=True, blank=True)
id_client = models.ForeignKey(Client)
My views.py :
from django.shortcuts import render
from http.client import HTTPConnection
from urllib.parse import urlparse, urlunparse
from .form import CommandesForm, CommandeForm
import requests
def resultat(request):
return render(request, 'commands/resultat.html')
def recherche(request):
if request.method == 'POST':
if 'Commandes' in request.POST:
pass
if 'Commande' in request.POST:
pass
else:
formCommandes = CommandesForm()
formCommande = CommandeForm()
return render(request, 'commands/recherche.html', {'formCommandes': formCommandes })
And my form.py:
from django import forms
from .models import Commande, Commandes
class CommandesForm(forms.ModelForm):
class Meta:
model = Commandes()
fields = ('date_debut', 'date_fin', 'id_groups')
class CommandeForm(forms.ModelForm):
class Meta:
model = Commande()
fields = ('date_debut', 'date_fin', 'id_groups', 'id_client', 'id_flux',
'id_commande')
This is the following exception that i need to manage :
'Commande' object has no attribute 'name'
I know that the 'name' attribute is in the class not the instance.
The fact that i can't figure out is, why the exception is raise with 'Commande' object and not 'Commands' object

Categories