El 12/03/11 13:31, yongzhen zhang escribió: > Hi, i want to rendering JSON from Django views, here is my models: > class Continent(models.Model): > name = models.CharField(max_length='10',unique=True) > code = models.CharField(max_length='5',unique=True) > class Country(models.Model): > name = models.CharField(max_length='10',unique=True) > code=models.CharField(max_length='10',unique=True) > continent=models.ForeignKey('Continent') > ###############here is my views.py####################### > from django.http import Http404 > from django.shortcuts import render_to_response, get_object_or_404 > from django.utils import simplejson as json > from models import Continent, Country > > def continent_json(request, continent_code): > continent=get_object_or_404(Continent, code=continent_code) > if not continent: > raise Http404("Not implemented") > context = {"all_contries": > Country.objects.filter(continent=continent.id)} > data=json.dumps(context) > return render_to_response(data, mimetype="application/json") > > I want to get the result when the continent_code=eu: > { > "xk": "Kosovo", > "ch": "Switzerland", > "gr": "Greece", > "va": "Vatican City", > "ee": "Estonia", > "is": "Iceland", > "al": "Albania", > "gg": "Guernsey", > } > There is some problems with my views.py file. Anybody know how to > modify my views.py? >
try with this view def continent_json(request, continent_code): continent=get_object_or_404(Continent, code=continent_code) if not continent: raise Http404("Not implemented") context = [{'code': i.code, 'continent': i.name } for i in Country.objects.all() ] data=simplejson.dumps(context) return render_to_response(data, mimetype="application/json") -- Ezequiel Marquez
signature.asc
Description: OpenPGP digital signature