Exemplo n.º 1
0
def cars_json(request): 
  response = {'cars': {}}
  if 'command' in request.GET:
    if request.GET['command'] == 'create':
      name = request.GET['name']
      new_car = Car(name=name, token=random_token(15))
      new_car.save()
      response['new_car'] = new_car.pk
  cars = Car.objects.all()
  for car in cars:
    response['cars'][car.pk] = (car.name, car.token)
  return HttpResponse(json.dumps(response))
Exemplo n.º 2
0
def cars(request):
  response = {}
  response.update(csrf(request))
  response['cars'] = Car.objects.all()
  if request.method == 'POST':
    command = request.POST['command']
    if command == 'create':
      name = request.POST['name']
      new_car = Car(name=name, token=random_token(15))
      new_car.save()
    elif command == 'delete':
      car = Car.objects.get(pk=request.POST['id'])
      car.delete()
  return render_to_response('cars.html', response)