Using django: Django post with data returns a HTTP 500 error on newest questions tagged django – Stack Overflow

I have started an app to post data to django using tastypie. However, when I send a post method such as

http://localhost:8000/api/recipes/item_new?format=json { data:
        {
          name: "Something",
          content: "Anything"
        }
}

I am getting a http error 500. Could you please help me to bypass that error ?

P.S= I am using chrome postman plugin to send post method above….

models.py

from django.db import models

import os
import sqlite3

class Recipe(models.Model):
    name = models.CharField(max_length=50)
    content = models.TextField()

    def __unicode__(self):
        return self.name

api.py

from tastypie.resources import ModelResource
from recipes.models import Recipe

class RecipeResource(ModelResource):
    class Meta:
        queryset = Recipe.objects.all()
        resource_name = 'recipes'
    allowed_methods = ['get', 'post']

def alter_list_data_to_serialize(self, request, data_dict):
    if isinstance(data_dict, dict):
        if 'meta' in data_dict:
            #Get rid of the meta object
            del(data_dict['meta'])

    return data_dict

views.py

import django.utils.simplejson as json
from django.http import HttpResponse, HttpResponseBadRequest
from tastypie import serializers
from recipes.models import Recipe

def item_new(request):
    # the request type has to be POST
    if request.method != "POST":
        return HttpResponseBadRequest("Request has to be of type POST")
    postdata = request.POST[u"data"]
    postdata = json.loads(postdata)
    item = Recipe()
    item.name = postdata["name"]
    item.content = postdata["content"]
    item.save()
    data = serializers.serialize("json", [item])
    return HttpResponse(data, mimetype="application/json")

urls.py

from django.conf.urls import *
from recipes.api import RecipeResource
from recipes import views
from django.contrib import admin

admin.autodiscover()

recipe_resource = RecipeResource()

urlpatterns = patterns('',
    url(r'^api/', include(recipe_resource.urls)),
    url(r'^api/recipes/$', views.item_new()),
    url(r'^admin/', include(admin.site.urls)),
)

See Answers


source: http://stackoverflow.com/questions/11812117/django-post-with-data-returns-a-http-500-error
Using django: using-django



online applications demo